Skip to content

Commit 6b0583a

Browse files
committed
[20250910] BOJ / G2 / 중앙값 구하기 / 김민진
1 parent e2fef48 commit 6b0583a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
import java.io.*;
3+
import java.util.PriorityQueue;
4+
import java.util.Queue;
5+
import java.util.StringTokenizer;
6+
7+
public class BJ_2696_중앙값_구하기 {
8+
9+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
private static StringTokenizer st;
12+
13+
private static int T;
14+
private static int M;
15+
private static String s;
16+
17+
private static Queue<Integer> minq;
18+
private static Queue<Integer> maxq;
19+
20+
public static void main(String[] args) throws IOException {
21+
sol();
22+
}
23+
24+
private static void sol() throws IOException {
25+
T = Integer.parseInt(br.readLine());
26+
27+
while (T-- > 0) {
28+
M = Integer.parseInt(br.readLine());
29+
30+
s = "";
31+
minq = new PriorityQueue<>();
32+
maxq = new PriorityQueue<>((a, b) -> b - a);
33+
for (int i = 0; i < M; i++) {
34+
if (i % 10 == 0) {
35+
st = new StringTokenizer(br.readLine());
36+
}
37+
38+
if (i % 2 == 0) {
39+
maxq.offer(Integer.parseInt(st.nextToken()));
40+
} else {
41+
minq.offer(Integer.parseInt(st.nextToken()));
42+
}
43+
44+
if (!minq.isEmpty() && maxq.peek() > minq.peek()) {
45+
minq.offer(maxq.poll());
46+
maxq.offer(minq.poll());
47+
}
48+
49+
if (i % 2 == 0) {
50+
s += maxq.peek() + (i > 2 && (i + 2) % 20 == 0 ? "\n" : " ");
51+
}
52+
}
53+
bw.write(maxq.size() + "\n");
54+
bw.write(s + "\n");
55+
}
56+
bw.flush();
57+
bw.close();
58+
br.close();
59+
}
60+
61+
}
62+
```

0 commit comments

Comments
 (0)