-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1655.java
More file actions
40 lines (32 loc) · 1.3 KB
/
boj1655.java
File metadata and controls
40 lines (32 loc) · 1.3 KB
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
38
39
40
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class boj1655 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> maxQueue = new PriorityQueue<>(Comparator.reverseOrder());
PriorityQueue<Integer> minQueue = new PriorityQueue<>();
for (int i = 0; i < N; i++) {
int num = Integer.parseInt(br.readLine());
if (maxQueue.size() == minQueue.size()) {
maxQueue.offer(num);
if (!minQueue.isEmpty() && maxQueue.peek() > minQueue.peek()) {
minQueue.offer(maxQueue.poll());
maxQueue.offer(minQueue.poll());
}
} else {
minQueue.offer(num);
if (maxQueue.peek() > minQueue.peek()) {
minQueue.offer(maxQueue.poll());
maxQueue.offer(minQueue.poll());
}
}
sb.append(maxQueue.peek()).append("\n");
}
System.out.println(sb);
}
}