Skip to content

Commit f707977

Browse files
authored
[20260224] BOJ / G2 / 중앙값 구하기 / 이준희
1 parent 38b83f4 commit f707977

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int T = Integer.parseInt(br.readLine());
9+
10+
StringBuilder sb = new StringBuilder();
11+
while (T-- > 0) {
12+
int M = Integer.parseInt(br.readLine());
13+
sb.append((M + 1) / 2).append("\n");
14+
15+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
16+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
17+
18+
int count = 0;
19+
StringTokenizer st = null;
20+
21+
for (int i = 0; i < M; i++) {
22+
if (i % 10 == 0) {
23+
st = new StringTokenizer(br.readLine());
24+
}
25+
26+
int num = Integer.parseInt(st.nextToken());
27+
28+
if (maxHeap.size() == minHeap.size()) maxHeap.add(num);
29+
else minHeap.add(num);
30+
31+
if (!minHeap.isEmpty() && maxHeap.peek() > minHeap.peek()) {
32+
int tmp1 = maxHeap.poll();
33+
int tmp2 = minHeap.poll();
34+
maxHeap.add(tmp2);
35+
minHeap.add(tmp1);
36+
}
37+
38+
if (i % 2 == 0) {
39+
sb.append(maxHeap.peek()).append(" ");
40+
count++;
41+
if (count % 10 == 0) sb.append("\n");
42+
}
43+
}
44+
if (count % 10 != 0) sb.append("\n");
45+
}
46+
System.out.print(sb.toString());
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)