-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1655.java
More file actions
29 lines (21 loc) · 897 Bytes
/
BOJ_1655.java
File metadata and controls
29 lines (21 loc) · 897 Bytes
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
import java.io.*;
import java.util.*;
public class BOJ_1655 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq1 = new PriorityQueue<>((a, b) -> b-a);
PriorityQueue<Integer> pq2 = new PriorityQueue<>();
for(int i = 0; i < n; i++) {
int num = Integer.parseInt(br.readLine());
if(pq1.isEmpty() || pq1.peek() >= num) pq1.add(num);
else pq2.add(num);
// 큐 균형 맞추기
if(pq1.size() == pq2.size()+2) pq2.add(pq1.poll());
else if(pq1.size()+1 == pq2.size()) pq1.add(pq2.poll());
sb.append(pq1.peek()).append("\n");
}
System.out.println(sb);
}
}