diff --git a/syheo/baekjoon_java/Main_10800.java b/syheo/baekjoon_java/Main_10800.java new file mode 100644 index 0000000..fc4fa9f --- /dev/null +++ b/syheo/baekjoon_java/Main_10800.java @@ -0,0 +1,141 @@ +package syheo.baekjoon_java; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +/** + * solved.ac + * 백준 + * 10800 + * 컬러볼 + * 정렬 , 구현 + * 골드 3 + * 아이디어 : + * 공의 무게->색깔로 오름차순 정렬 후에 + * 색깔별 공의 무게의 누적합을 저장하는 배열의 값을 통해 정답 배열을 계산하여 채워나간다. + * 색깔이 바뀌는 경우, 무게가 바뀌는 경우를 분기로 설정하여 현재 무게 및 색깔을 설정한다. + * **/ + +public class Main_10800 { + + static class Ball implements Comparable{ + int num; + int weight; + int color; + + public Ball(int num, int weight, int color) { + this.num = num; + this.weight = weight; + this.color = color; + } + + @Override + public int compareTo(Ball o) { + if(this.weight > o.weight){ + return 1; + } + else if(this.weight < o.weight){ + return -1; + } + else{ + return Integer.compare(this.color, o.color); + } + } + + @Override + public String toString() { + return "Ball{" + + "num=" + num + + ", weight=" + weight + + ", color=" + color + + '}'; + } + } + + static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer tokens; + static List balls = new ArrayList(); + static int N; + static int[] answers; + static int[] colorSums; + + public static void main(String[] args) throws IOException { + + // 1. input + N = Integer.parseInt(input.readLine()); + for (int i = 0; i < N; i++) { + tokens = new StringTokenizer(input.readLine()); + int color = Integer.parseInt(tokens.nextToken()); + int weight = Integer.parseInt(tokens.nextToken()); + balls.add(new Ball(i,weight,color)); + } + + // 2. sort 오름차순 + Collections.sort(balls); + balls.stream().forEach(ball -> System.out.println(ball.toString())); + + // 3. Allocate array + answers = new int[N+1]; + colorSums = new int[N+1]; + + //4. save weight sum + int weightSum = 0; + int nowWeight = balls.get(0).weight; + int nowColor = balls.get(0).color; + int nowColorSum = 0; + int nowWeightSum = 0; + boolean isChanged = false; + for (int i = 0; i < N; i++) { + isChanged = false; + //무게가 바뀐 경우 + if(isWeightChanged(nowWeight,balls.get(i).weight)){ + // 현재 무게값 초기화 + weightSum += nowWeightSum; + nowWeight = balls.get(i).weight; + nowWeightSum = balls.get(i).weight; + // 현재 색깔값 초기화 + colorSums[nowColor]+=nowColorSum; + nowColor = balls.get(i).color; + nowColorSum = balls.get(i).weight; + isChanged = true; + } + else{ + nowWeightSum += balls.get(i).weight; + } + answers[balls.get(i).num] = weightSum - colorSums[balls.get(i).color]; + //무게가 바뀌지 않은 경우에만 체크 + if(!isChanged){ + //색깔이 바뀌었을 경우 + if(isColorChanged(nowColor,balls.get(i).color)){ + colorSums[nowColor]+=nowColorSum; + nowColor = balls.get(i).color; + nowColorSum = balls.get(i).weight; + } + else{ + nowColorSum += balls.get(i).weight; + } + } + + } + + // 5. print + for (int i = 0; i < N; i++) { + System.out.println(answers[i]); + } + + + } + + private static boolean isWeightChanged(int nowWeight, int weight) { + return nowWeight != weight; + } + + private static boolean isColorChanged(int nowColor, int color) { + return nowColor != color; + } +} diff --git a/syheo/baekjoon_java/Main_12927.java b/syheo/baekjoon_java/Main_12927.java new file mode 100644 index 0000000..a7a8083 --- /dev/null +++ b/syheo/baekjoon_java/Main_12927.java @@ -0,0 +1,57 @@ +package syheo.baekjoon_java; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +/** + * solved.ac + * 백준 + * 12927 + * 배수 스위치 + * 그리디 + * 실버 4 + * 아이디어 : + * 켜진 전구 중 번호가 제일 작은 전구부터 끈다. + * 의문 : + * 근데 이거 무조건 다 끌 수 있지 않나? 못 끄는 경우는 없는 것 같다. + */ + +public class Main_12927 { + + static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer tokens; + static String inputLights; + static int answer = 0; + static int minIdx = 1; + static boolean[] lights; + + public static void main(String[] args) throws IOException { + + //1. input + inputLights = input.readLine(); + lights = new boolean[inputLights.length()+1]; + for (int i = 0; i < inputLights.length(); i++) { + if (inputLights.charAt(i) == 'N') { + lights[i+1] = false; + } else { + lights[i+1] = true; + } + } + + //2. 제일 작은 숫자부터 스위치 끄기 + while(minIdx < lights.length){ + if(lights[minIdx]==true){ + for (int i = minIdx; i < lights.length; i+=minIdx) { + lights[i] = !lights[i]; + } + answer++; + } + minIdx++; + } + + System.out.println(answer); + + } +} diff --git a/syheo/baekjoon_java/Main_2533.java b/syheo/baekjoon_java/Main_2533.java new file mode 100644 index 0000000..74ede04 --- /dev/null +++ b/syheo/baekjoon_java/Main_2533.java @@ -0,0 +1,97 @@ +package syheo.baekjoon_java; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main_2533 { + + static class Node{ + boolean isEarlyAdapter; + int parent; + List childNodes; + + public Node(boolean isEarlyAdapter, int parent, List childNodes) { + this.isEarlyAdapter = isEarlyAdapter; + this.parent = parent; + this.childNodes = childNodes; + } + + @Override + public String toString() { + return "Node{" + + "isEarlyAdapter=" + isEarlyAdapter + + ", parent=" + parent + + ", childNodes=" + childNodes + + '}'; + } + } + + static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer tokens; + static Queue queue = new LinkedList<>(); + static int headNum; + static Node[] nodes; + static int N; + static int answer = 0; + + + public static void main(String[] args) throws IOException { + + // 1. input + N = Integer.parseInt(input.readLine()); + + nodes = new Node[N+1]; + for (int i = 0; i <= N; i++) { + nodes[i] = new Node(false,-1,new ArrayList()); + } + for (int i = 0; i < N-1; i++) { + tokens = new StringTokenizer(input.readLine()); + int u = Integer.parseInt(tokens.nextToken()); + int v = Integer.parseInt(tokens.nextToken()); + if(u>v){ + int tmp = v; + v = u; + u = v; + } + //root 설정 + if(i==0){ + headNum = u; + } + //부모 자식 설정 + nodes[v].parent = u; + nodes[u].childNodes.add(v); + } + + // 2. solve + queue.add(headNum); + int nowNode; + int cnt = 0; + while(!queue.isEmpty()){ + cnt++; + nowNode = queue.poll(); + if(nodes[nowNode].parent!=-1){ + if(!nodes[nodes[nowNode].parent].isEarlyAdapter){ + nodes[nowNode].isEarlyAdapter = true; + answer++; + } + else if(nodes[nowNode].childNodes.size()>=2){ + nodes[nowNode].isEarlyAdapter = true; + answer++; + } + } + for (int i = 0; i < nodes[nowNode].childNodes.size(); i++) { + if(!nodes[nowNode].isEarlyAdapter || nodes[nodes[nowNode].childNodes.get(i)].childNodes.size()!=0) + queue.add(nodes[nowNode].childNodes.get(i)); + } + } + System.out.println(cnt+"adf"); +// for (int i = 0; i < N; i++) { +// System.out.println(i+"번 노드: "+nodes[i].toString()); +// } + + // 3. output + System.out.println(answer); + } +}