https://www.acmicpc.net/problem/1655
중간값을 기준으로 작은 값은 maxHeap, 큰값은 minHeap에 저장한다. 중간값은 언제나 minHeap에 저장한다.
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
private static PriorityQueue<Integer> right = new PriorityQueue<>();
private static PriorityQueue<Integer> left = new PriorityQueue<>(Comparator.reverseOrder());
public static void main(String[] args) throws IOException {
int N = Integer.parseInt(reader.readLine());
StringBuilder result = new StringBuilder();
// i = 0
left.add(Integer.parseInt(reader.readLine()));
result.append(left.peek()).append(System.lineSeparator());
if (N < 2) {
System.out.print(result);
return;
}
// i = 1
Integer t = Integer.parseInt(reader.readLine());
if (left.peek() <= t) {
right.add(t);
} else {
right.add(left.poll());
left.add(t);
}
result.append(left.peek()).append(System.lineSeparator());
for (int i = 2; i < N; i++) {
Integer number = Integer.parseInt(reader.readLine());
Integer leftMiddle = left.peek();
Integer rightMiddle = right.peek();
if (i % 2 == 0) {
if (number <= rightMiddle) {
left.add(number);
} else {
left.add(right.poll());
right.add(number);
}
} else {
if (number < leftMiddle) {
right.add(left.poll());
left.add(number);
} else {
right.add(number);
}
}
result.append(left.peek()).append(System.lineSeparator());
}
System.out.print(result);
}
}