반응형
BOJ 1655 풀이 코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> maxq = new PriorityQueue<>((o1, o2) -> (o2 - o1));
PriorityQueue<Integer> minq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
int num = Integer.parseInt(br.readLine());
if(maxq.size() == minq.size()) {
maxq.add(num);
}
else {
minq.add(num);
}
if(!maxq.isEmpty() && !minq.isEmpty()) {
if(minq.peek() < maxq.peek()) {
int tmp = minq.poll();
minq.add(maxq.poll());
maxq.add(tmp);
}
}
bw.write(maxq.peek() + "\n");
}
bw.flush();
br.close();
bw.close();
}
}
반응형
'코딩 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 자바 문제 풀이 1275 : 골드1 (0) | 2023.09.26 |
---|---|
[백준] 자바 문제 풀이 5676 : 골드1 (0) | 2023.09.26 |
[백준] 자바 문제 풀이 10868 : 골드1 (0) | 2023.09.23 |
[백준] 자바 문제 풀이 10775 : 골드2 (0) | 2023.09.23 |
[백준] 자바 문제 풀이 11505 : 골드1 (0) | 2023.09.23 |