
우선 처음에는 HashSet을 사용하여 min,max 큐를 각각 동기화해 주었는데 실패했다. 그래서 구글링을 통해 HashMap을 사용해 풀었다.
import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
PriorityQueue<Integer> min = new PriorityQueue<>();
PriorityQueue<Integer> max = new PriorityQueue<>(Collections.reverseOrder());
HashMap<Integer, Integer> count = new HashMap<>();
int k = Integer.parseInt(br.readLine());
for (int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());
String s = st.nextToken();
int n = Integer.parseInt(st.nextToken());
if (s.equals("I")) {
min.offer(n);
max.offer(n);
count.put(n, count.getOrDefault(n, 0) + 1);
} else {
if (n == 1) {
while (!max.isEmpty() && count.getOrDefault(max.peek(), 0) == 0) {
max.poll();
}
if (!max.isEmpty()) {
int value = max.poll();
count.put(value, count.get(value) - 1);
}
} else {
while (!min.isEmpty() && count.getOrDefault(min.peek(), 0) == 0) {
min.poll();
}
if (!min.isEmpty()) {
int value = min.poll();
count.put(value, count.get(value) - 1);
}
}
}
}
while (!max.isEmpty() && count.getOrDefault(max.peek(), 0) == 0) {
max.poll();
}
while (!min.isEmpty() && count.getOrDefault(min.peek(), 0) == 0) {
min.poll();
}
if (!max.isEmpty() && !min.isEmpty()) {
sb.append(max.peek()).append(" ").append(min.peek()).append("\n");
} else {
sb.append("EMPTY\n");
}
}
System.out.print(sb);
}
}

import java.util.*;
import java.io.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while(t-->0){
PriorityQueue<Integer> min = new PriorityQueue<>();
PriorityQueue<Integer> max = new PriorityQueue<>(Collections.reverseOrder());
HashSet<Integer> check = new HashSet<>();
int k = Integer.parseInt(br.readLine());
for(int i=0;i<k;i++){
st = new StringTokenizer(br.readLine());
String s = st.nextToken();
int n = Integer.parseInt(st.nextToken());
if(s.equals("I")){
min.offer(n);
max.offer(n);
check.add(n);
}else{
if(n==1){
while(!max.isEmpty() && !check.contains(max.peek())){
max.poll();
}
if(!max.isEmpty()){
check.remove(max.poll());
}
}else{
while(!min.isEmpty() && !check.contains(min.peek())){
min.poll();
}
if(!min.isEmpty()){
check.remove(min.poll());
}
}
}
}
while (!max.isEmpty() && !check.contains(max.peek())) {
max.poll();
}
while (!min.isEmpty() && !check.contains(min.peek())) {
min.poll();
}
if(!max.isEmpty() && !min.isEmpty()){
sb.append(max.peek()+" "+min.peek()+"\n");
}else{
sb.append("EMPTY\n");
}
}
System.out.print(sb);
}
}