acmicpc
[BAEKJOON] 1655. 가운데를 말해요
코드와이
2021. 5. 4. 00:50
문제링크
1655번: 가운데를 말해요
첫째 줄에는 수빈이가 외치는 정수의 개수 N이 주어진다. N은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수이다. 그 다음 N줄에 걸쳐서 수빈이가 외치는 정수가 차례대로 주어진다. 정수는 -1
www.acmicpc.net
package acmicpc.Gold2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class 가운데를_말해요 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pqMin = new PriorityQueue<Integer>((o1 ,o2) -> o2 - o1);
PriorityQueue<Integer> pqMax = new PriorityQueue<Integer>();
for(int i = 0 ; i < n ; i ++) {
int x = Integer.parseInt(br.readLine());
if(pqMin.size() == pqMax.size()) {
pqMin.add(x);
if(!pqMax.isEmpty() && pqMin.peek() > pqMax.peek()) {
pqMax.add(pqMin.poll());
pqMin.add(pqMax.poll());
}
} else {
pqMax.add(x);
if(pqMin.peek() > pqMax.peek()) {
pqMax.add(pqMin.poll());
pqMin.add(pqMax.poll());
}
}
sb.append(pqMin.peek()).append("\n");
}
sb.setLength(sb.length()-1);
System.out.println(sb);
}
}