알고리즘
백준 2535 아시아 정보올림피아드
킨글
2024. 7. 21. 19:38
설명
PriorityQueue를 이용해서 높은 점수부터 출력하도록 했다. class를 이용해서 높은 점수를 받은 사람의 나라와 번호를 저장했다.
그리고 2번 이상 메달을 받을 수 없기 때문에 나라별로 중복을 어떻게 처리할까 싶어서 countryMap을 추가로 선언해서 메달을 받을 때마다 value 값을 더해주고, 2번을 받으면 poll만 하고 다시 while 문을 돌도록 처리했다.
코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
// 2535
public static void main(String[] args) throws IOException {
process();
}
private static void process() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
PriorityQueue<People> priorityQueue = new PriorityQueue<>((o1, o2) -> Integer.compare(o2.score, o1.score));
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int country = Integer.parseInt(st.nextToken());
int num = Integer.parseInt(st.nextToken());
int score = Integer.parseInt(st.nextToken());
priorityQueue.add(new People(country, num, score));
}
Map<Integer, Integer> countryMap = new HashMap<>();
int resultCnt = 0;
while(true) {
People people = priorityQueue.poll();
Integer countryCnt = countryMap.getOrDefault(people.country, 0);
if(countryCnt != 2) {
countryMap.put(people.country, countryCnt+1);
bw.write(people.country + " " + people.num + "\n");
resultCnt++;
}
if(resultCnt == 3) {
break;
}
}
bw.flush();
bw.close();
}
static class People {
int country;
int num;
int score;
public People(int country, int num, int score) {
this.country = country;
this.num = num;
this.score = score;
}
}
}