-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1655.java
37 lines (27 loc) · 1.02 KB
/
1655.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int n = 1; n <= N; n++) {
int I = Integer.parseInt(br.readLine());
if (n % 2 == 1) {
maxHeap.add(-I);
} else {
minHeap.add(I);
}
if (!minHeap.isEmpty() && -maxHeap.peek() > minHeap.peek()) {
int max = -maxHeap.remove();
int min = minHeap.remove();
maxHeap.add(-min);
minHeap.add(max);
}
bw.write(-maxHeap.peek() + "\n");
}
bw.close();
}
}