Skip to content

Commit ecacf46

Browse files
authored
Merge pull request #863 from AlgorithmWithGod/zinnnn37
[20250910] BOJ / G2 / 중앙값 구하기 / 김민진
2 parents 2c66ff5 + 02b42aa commit ecacf46

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+
bw.write((M / 2 + 1) + "\n");
30+
31+
s = "";
32+
minq = new PriorityQueue<>();
33+
maxq = new PriorityQueue<>((a, b) -> b - a);
34+
for (int i = 0; i < M; i++) {
35+
if (i % 10 == 0) {
36+
st = new StringTokenizer(br.readLine());
37+
}
38+
39+
if (i % 2 == 0) {
40+
maxq.offer(Integer.parseInt(st.nextToken()));
41+
} else {
42+
minq.offer(Integer.parseInt(st.nextToken()));
43+
}
44+
45+
if (!minq.isEmpty() && maxq.peek() > minq.peek()) {
46+
minq.offer(maxq.poll());
47+
maxq.offer(minq.poll());
48+
}
49+
50+
if (i % 2 == 0) {
51+
bw.write(maxq.peek() + (i > 2 && (i + 2) % 20 == 0 ? "\n" : " "));
52+
}
53+
}
54+
bw.write("\n");
55+
}
56+
bw.flush();
57+
bw.close();
58+
br.close();
59+
}
60+
61+
}
62+
```

0 commit comments

Comments
 (0)