Skip to content

Commit fe8e6ef

Browse files
authored
Merge pull request #914 from AlgorithmWithGod/zinnnn37
[20250917] BOJ / G4 / 이중 우선순위 큐 / 김민진
2 parents 06c3785 + e311467 commit fe8e6ef

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BJ_7662_이중_우선순위_큐 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static final StringBuilder sb = new StringBuilder();
10+
private static StringTokenizer st;
11+
12+
private static int N;
13+
14+
private static PriorityQueue<Integer> minHeap = new PriorityQueue<>();
15+
private static PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
16+
private static Map<Integer, Integer> count = new HashMap<>();
17+
18+
public static void main(String[] args) throws IOException {
19+
int T = Integer.parseInt(br.readLine());
20+
21+
while (T-- > 0) {
22+
solve();
23+
}
24+
25+
bw.write(sb.toString());
26+
bw.flush();
27+
bw.close();
28+
br.close();
29+
}
30+
31+
private static void solve() throws IOException {
32+
N = Integer.parseInt(br.readLine());
33+
34+
clear();
35+
36+
for (int i = 0; i < N; i++) {
37+
st = new StringTokenizer(br.readLine());
38+
39+
String command = st.nextToken();
40+
int value = Integer.parseInt(st.nextToken());
41+
42+
if (command.equals("I")) {
43+
insert(value);
44+
} else {
45+
delete(value == 1 ? maxHeap : minHeap);
46+
}
47+
}
48+
49+
printResult();
50+
}
51+
52+
private static void insert(int value) {
53+
minHeap.offer(value);
54+
maxHeap.offer(value);
55+
count.put(value, count.getOrDefault(value, 0) + 1);
56+
}
57+
58+
private static void delete(Queue<Integer> heap) {
59+
while (!heap.isEmpty()) {
60+
int current = heap.poll();
61+
int currentCount = count.getOrDefault(current, 0);
62+
63+
if (currentCount == 0) continue;
64+
65+
if (currentCount == 1) {
66+
count.remove(current);
67+
} else {
68+
count.put(current, currentCount - 1);
69+
}
70+
break;
71+
}
72+
}
73+
74+
private static void printResult() {
75+
if (count.isEmpty()) {
76+
sb.append("EMPTY\n");
77+
return;
78+
}
79+
80+
cleanHeap(maxHeap);
81+
cleanHeap(minHeap);
82+
83+
int max = maxHeap.poll();
84+
int min = minHeap.poll();
85+
86+
sb.append(max).append(" ").append(min).append("\n");
87+
}
88+
89+
private static void cleanHeap(Queue<Integer> heap) {
90+
while (!heap.isEmpty() && count.getOrDefault(heap.peek(), 0) == 0) {
91+
heap.poll();
92+
}
93+
}
94+
95+
private static void clear() {
96+
minHeap.clear();
97+
maxHeap.clear();
98+
count.clear();
99+
}
100+
}
101+
```

0 commit comments

Comments
 (0)