-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_1655.java
More file actions
71 lines (55 loc) · 1.89 KB
/
baekjoon_1655.java
File metadata and controls
71 lines (55 loc) · 1.89 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
import java.io.*;
public class baekjoon_1655 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for(int i=0;i<N;i++) {
arr[i] = Integer.parseInt(br.readLine());
}
PriorityQueue<Integer> small = new PriorityQueue<>(Comparator.reverseOrder());
PriorityQueue<Integer> big = new PriorityQueue<>();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i=0;i<N;i++) {
int num = arr[i];
if(i == 0) {
small.add(num);
bw.write(num + "\n");
// print(small, big);
continue;
}
else if(i == 1) {
if(small.peek() < num) big.add(num);
else {
small.add(num);
big.add(small.poll());
}
// print(small, big);
bw.write( Math.min(small.peek(), big.peek()) + "\n");
continue;
}
if(small.peek() < num) {
big.add(num);
}
else {
small.add(num);
}
if(small.size() < big.size()) {
small.add(big.poll());
}
else if (small.size() > big.size() + 1){
big.add(small.poll());
}
// print(small, big);
bw.write(small.peek() + "\n");
}
bw.flush();
bw.close();
}
public static void print(PriorityQueue a, PriorityQueue b) {
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println();
}
}