Skip to content

Commit 8880135

Browse files
authored
Merge pull request #1031 from AlgorithmWithGod/0224LJH
[20251003] BOJ / G4 / 이중 우선순위 큐 / 이종환
2 parents ae5d725 + 79ac162 commit 8880135

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
public class Main {
8+
9+
static final String DELETE = "D";
10+
static final int MIN= -1;
11+
static final int MAX = 1;
12+
13+
static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));;
14+
static StringBuilder sb = new StringBuilder();
15+
static int orderCnt,curIdx;
16+
17+
static PriorityQueue<Node> minPq = new PriorityQueue<>();
18+
static PriorityQueue<Node> maxPq = new PriorityQueue<>(Collections.reverseOrder());
19+
20+
static class Node implements Comparable<Node>{
21+
int idx;
22+
int num;
23+
boolean isAlive = true;
24+
25+
public Node(int num) {
26+
this.num = num;
27+
this.idx = curIdx++;
28+
}
29+
30+
public int compareTo(Node n) {
31+
if ( this.num == n.num) {
32+
return Integer.compare(this.idx, n.idx);
33+
}
34+
return Integer.compare(this.num, n.num);
35+
}
36+
37+
}
38+
39+
40+
41+
42+
public static void main(String[] args) throws NumberFormatException, IOException {
43+
int TC = Integer.parseInt(br.readLine());
44+
for (int tc = 1 ; tc <= TC; tc++) {
45+
init();
46+
process();
47+
48+
}
49+
print();
50+
}
51+
52+
53+
54+
public static void init() throws NumberFormatException, IOException {
55+
orderCnt = Integer.parseInt(br.readLine());
56+
minPq.clear();
57+
maxPq.clear();
58+
curIdx=0;
59+
}
60+
61+
public static void process() throws IOException {
62+
for (int i = 0; i < orderCnt; i++) {
63+
StringTokenizer st = new StringTokenizer(br.readLine());
64+
String order = st.nextToken();
65+
int num = Integer.parseInt(st.nextToken());
66+
if (order.equals(DELETE)) {
67+
// 삭제
68+
if (num == MIN) {
69+
while(!minPq.isEmpty()) {
70+
Node n = minPq.poll();
71+
if (n.isAlive) {
72+
n.isAlive = false;
73+
break;
74+
}
75+
}
76+
77+
} else if (num == MAX) {
78+
while(!maxPq.isEmpty()) {
79+
Node n = maxPq.poll();
80+
if (n.isAlive) {
81+
n.isAlive = false;
82+
break;
83+
}
84+
}
85+
}
86+
87+
88+
} else {
89+
//삽입
90+
Node n = new Node(num);
91+
minPq.add(n);
92+
maxPq.add(n);
93+
}
94+
}
95+
96+
97+
while(!minPq.isEmpty() && !minPq.peek().isAlive) {
98+
minPq.poll();
99+
}
100+
101+
while(!maxPq.isEmpty() && !maxPq.peek().isAlive) {
102+
maxPq.poll();
103+
}
104+
105+
if (maxPq.isEmpty()) {
106+
sb.append("EMPTY").append("\n");
107+
} else {
108+
sb.append(maxPq.poll().num).append(" ").append(minPq.poll().num).append("\n");
109+
}
110+
111+
}
112+
113+
114+
115+
116+
public static void print() {
117+
System.out.print(sb);
118+
119+
}
120+
}
121+
```

0 commit comments

Comments
 (0)