-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
35 lines (29 loc) Β· 1.22 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Heap.P7662;
import java.io.*;
import java.util.*;
public class Main {
static int T, k;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Heap/P7662/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
while (T-- > 0) {
k = Integer.parseInt(br.readLine());
TreeMap<Integer, Integer> map = new TreeMap<>();
while (k-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
char cmd = st.nextToken().charAt(0);
int n = Integer.parseInt(st.nextToken());
if (cmd == 'I') {
map.merge(n, 1, Integer::sum);
} else if (cmd == 'D' && !map.isEmpty()) {
int target = (n == 1) ? map.lastKey() : map.firstKey();
if (map.get(target) > 1) map.put(target, map.get(target)-1);
else map.remove(target);
}
}
if (map.isEmpty()) System.out.println("EMPTY");
else System.out.println(map.lastKey() + " " + map.firstKey());
}
}
}