Skip to content

Commit d7027b3

Browse files
committed
2533, 10800, 12927
1 parent 59ec40f commit d7027b3

File tree

3 files changed

+295
-0
lines changed

3 files changed

+295
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.StringTokenizer;
10+
11+
/**
12+
* solved.ac
13+
* 백준
14+
* 10800
15+
* 컬러볼
16+
* 정렬 , 구현
17+
* 골드 3
18+
* 아이디어 :
19+
* 공의 무게->색깔로 오름차순 정렬 후에
20+
* 색깔별 공의 무게의 누적합을 저장하는 배열의 값을 통해 정답 배열을 계산하여 채워나간다.
21+
* 색깔이 바뀌는 경우, 무게가 바뀌는 경우를 분기로 설정하여 현재 무게 및 색깔을 설정한다.
22+
* **/
23+
24+
public class Main_10800 {
25+
26+
static class Ball implements Comparable<Ball>{
27+
int num;
28+
int weight;
29+
int color;
30+
31+
public Ball(int num, int weight, int color) {
32+
this.num = num;
33+
this.weight = weight;
34+
this.color = color;
35+
}
36+
37+
@Override
38+
public int compareTo(Ball o) {
39+
if(this.weight > o.weight){
40+
return 1;
41+
}
42+
else if(this.weight < o.weight){
43+
return -1;
44+
}
45+
else{
46+
return Integer.compare(this.color, o.color);
47+
}
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Ball{" +
53+
"num=" + num +
54+
", weight=" + weight +
55+
", color=" + color +
56+
'}';
57+
}
58+
}
59+
60+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
61+
static StringTokenizer tokens;
62+
static List<Ball> balls = new ArrayList<Ball>();
63+
static int N;
64+
static int[] answers;
65+
static int[] colorSums;
66+
67+
public static void main(String[] args) throws IOException {
68+
69+
// 1. input
70+
N = Integer.parseInt(input.readLine());
71+
for (int i = 0; i < N; i++) {
72+
tokens = new StringTokenizer(input.readLine());
73+
int color = Integer.parseInt(tokens.nextToken());
74+
int weight = Integer.parseInt(tokens.nextToken());
75+
balls.add(new Ball(i,weight,color));
76+
}
77+
78+
// 2. sort 오름차순
79+
Collections.sort(balls);
80+
balls.stream().forEach(ball -> System.out.println(ball.toString()));
81+
82+
// 3. Allocate array
83+
answers = new int[N+1];
84+
colorSums = new int[N+1];
85+
86+
//4. save weight sum
87+
int weightSum = 0;
88+
int nowWeight = balls.get(0).weight;
89+
int nowColor = balls.get(0).color;
90+
int nowColorSum = 0;
91+
int nowWeightSum = 0;
92+
boolean isChanged = false;
93+
for (int i = 0; i < N; i++) {
94+
isChanged = false;
95+
//무게가 바뀐 경우
96+
if(isWeightChanged(nowWeight,balls.get(i).weight)){
97+
// 현재 무게값 초기화
98+
weightSum += nowWeightSum;
99+
nowWeight = balls.get(i).weight;
100+
nowWeightSum = balls.get(i).weight;
101+
// 현재 색깔값 초기화
102+
colorSums[nowColor]+=nowColorSum;
103+
nowColor = balls.get(i).color;
104+
nowColorSum = balls.get(i).weight;
105+
isChanged = true;
106+
}
107+
else{
108+
nowWeightSum += balls.get(i).weight;
109+
}
110+
answers[balls.get(i).num] = weightSum - colorSums[balls.get(i).color];
111+
//무게가 바뀌지 않은 경우에만 체크
112+
if(!isChanged){
113+
//색깔이 바뀌었을 경우
114+
if(isColorChanged(nowColor,balls.get(i).color)){
115+
colorSums[nowColor]+=nowColorSum;
116+
nowColor = balls.get(i).color;
117+
nowColorSum = balls.get(i).weight;
118+
}
119+
else{
120+
nowColorSum += balls.get(i).weight;
121+
}
122+
}
123+
124+
}
125+
126+
// 5. print
127+
for (int i = 0; i < N; i++) {
128+
System.out.println(answers[i]);
129+
}
130+
131+
132+
}
133+
134+
private static boolean isWeightChanged(int nowWeight, int weight) {
135+
return nowWeight != weight;
136+
}
137+
138+
private static boolean isColorChanged(int nowColor, int color) {
139+
return nowColor != color;
140+
}
141+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* solved.ac
10+
* 백준
11+
* 12927
12+
* 배수 스위치
13+
* 그리디
14+
* 실버 4
15+
* 아이디어 :
16+
* 켜진 전구 중 번호가 제일 작은 전구부터 끈다.
17+
* 의문 :
18+
* 근데 이거 무조건 다 끌 수 있지 않나? 못 끄는 경우는 없는 것 같다.
19+
*/
20+
21+
public class Main_12927 {
22+
23+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
24+
static StringTokenizer tokens;
25+
static String inputLights;
26+
static int answer = 0;
27+
static int minIdx = 1;
28+
static boolean[] lights;
29+
30+
public static void main(String[] args) throws IOException {
31+
32+
//1. input
33+
inputLights = input.readLine();
34+
lights = new boolean[inputLights.length()+1];
35+
for (int i = 0; i < inputLights.length(); i++) {
36+
if (inputLights.charAt(i) == 'N') {
37+
lights[i+1] = false;
38+
} else {
39+
lights[i+1] = true;
40+
}
41+
}
42+
43+
//2. 제일 작은 숫자부터 스위치 끄기
44+
while(minIdx < lights.length){
45+
if(lights[minIdx]==true){
46+
for (int i = minIdx; i < lights.length; i+=minIdx) {
47+
lights[i] = !lights[i];
48+
}
49+
answer++;
50+
}
51+
minIdx++;
52+
}
53+
54+
System.out.println(answer);
55+
56+
}
57+
}

syheo/baekjoon_java/Main_2533.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class Main_2533 {
9+
10+
static class Node{
11+
boolean isEarlyAdapter;
12+
int parent;
13+
List<Integer> childNodes;
14+
15+
public Node(boolean isEarlyAdapter, int parent, List<Integer> childNodes) {
16+
this.isEarlyAdapter = isEarlyAdapter;
17+
this.parent = parent;
18+
this.childNodes = childNodes;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "Node{" +
24+
"isEarlyAdapter=" + isEarlyAdapter +
25+
", parent=" + parent +
26+
", childNodes=" + childNodes +
27+
'}';
28+
}
29+
}
30+
31+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
32+
static StringTokenizer tokens;
33+
static Queue<Integer> queue = new LinkedList<>();
34+
static int headNum;
35+
static Node[] nodes;
36+
static int N;
37+
static int answer = 0;
38+
39+
40+
public static void main(String[] args) throws IOException {
41+
42+
// 1. input
43+
N = Integer.parseInt(input.readLine());
44+
45+
nodes = new Node[N+1];
46+
for (int i = 0; i <= N; i++) {
47+
nodes[i] = new Node(false,-1,new ArrayList<Integer>());
48+
}
49+
for (int i = 0; i < N-1; i++) {
50+
tokens = new StringTokenizer(input.readLine());
51+
int u = Integer.parseInt(tokens.nextToken());
52+
int v = Integer.parseInt(tokens.nextToken());
53+
if(u>v){
54+
int tmp = v;
55+
v = u;
56+
u = v;
57+
}
58+
//root 설정
59+
if(i==0){
60+
headNum = u;
61+
}
62+
//부모 자식 설정
63+
nodes[v].parent = u;
64+
nodes[u].childNodes.add(v);
65+
}
66+
67+
// 2. solve
68+
queue.add(headNum);
69+
int nowNode;
70+
int cnt = 0;
71+
while(!queue.isEmpty()){
72+
cnt++;
73+
nowNode = queue.poll();
74+
if(nodes[nowNode].parent!=-1){
75+
if(!nodes[nodes[nowNode].parent].isEarlyAdapter){
76+
nodes[nowNode].isEarlyAdapter = true;
77+
answer++;
78+
}
79+
else if(nodes[nowNode].childNodes.size()>=2){
80+
nodes[nowNode].isEarlyAdapter = true;
81+
answer++;
82+
}
83+
}
84+
for (int i = 0; i < nodes[nowNode].childNodes.size(); i++) {
85+
if(!nodes[nowNode].isEarlyAdapter || nodes[nodes[nowNode].childNodes.get(i)].childNodes.size()!=0)
86+
queue.add(nodes[nowNode].childNodes.get(i));
87+
}
88+
}
89+
System.out.println(cnt+"adf");
90+
// for (int i = 0; i < N; i++) {
91+
// System.out.println(i+"번 노드: "+nodes[i].toString());
92+
// }
93+
94+
// 3. output
95+
System.out.println(answer);
96+
}
97+
}

0 commit comments

Comments
 (0)