
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
int k = Integer.parseInt(br.readLine());
Map<Integer, Integer> hashMap = new HashMap<>();
PriorityQueue<Integer> max_heap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> min_heap = new PriorityQueue<>();
int count = 0;
for (int j = 0; j < k; j++) {
String[] input = br.readLine().split(" ");
char command = input[0].charAt(0);
int number = Integer.parseInt(input[1]);
if (command == 'I') {
// insert
max_heap.add(number);
min_heap.add(number);
hashMap.put(number, hashMap.getOrDefault(number, 0) + 1);
count++;
} else {
// delete
if (count == 0) {
continue;
}
if (number == -1) {
removeHeap(min_heap, hashMap);
} else {
removeHeap(max_heap, hashMap);
}
count--;
}
}
if (count == 0) {
bw.write("EMPTY\n");
} else {
cleanHeap(min_heap, hashMap);
cleanHeap(max_heap, hashMap);
bw.write(max_heap.peek() + " " + min_heap.peek() + "\n");
}
}
br.close();
bw.close();
}
private static void cleanHeap(PriorityQueue<Integer> queue, Map<Integer, Integer> hashMap) {
while (!queue.isEmpty()) {
int num = queue.peek();
if (hashMap.getOrDefault(num, 0) == 0) {
queue.poll();
} else {
break;
}
}
}
private static void removeHeap(PriorityQueue<Integer> queue, Map<Integer, Integer> hashMap) {
while (!queue.isEmpty()) {
int num = queue.poll();
if (hashMap.getOrDefault(num, 0) > 0) {
hashMap.put(num, hashMap.get(num) - 1);
break;
}
}
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 9019번: DSLR (0) | 2025.04.08 |
|---|---|
| [JAVA-자바] 16928번: 뱀과 사다리 게임 (0) | 2025.04.08 |
| [JAVA-자바] 10026번: 적록색약 (0) | 2025.04.04 |
| [JAVA-자바] 7569번: 토마토 (0) | 2025.04.04 |
| [JAVA-자바] 5430번: AC (0) | 2025.04.02 |