-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBOJ_7662.java
More file actions
53 lines (46 loc) · 1.38 KB
/
BOJ_7662.java
File metadata and controls
53 lines (46 loc) · 1.38 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package algorithm_study.april.day_04_16;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class BOJ_7662 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 > o2 ? 1 : -1;
}
});
int TC = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= TC; tc++) {
int k = Integer.parseInt(br.readLine());
for (int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());
char command = st.nextToken().charAt(0);
int value = Integer.parseInt(st.nextToken());
if(command == 'I') {
ts.add(value);
} else if(command == 'D') {
if(value == 1) {
ts.pollLast();
} else {
ts.pollFirst();
}
}
}
if(ts.size() == 0) {
System.out.println("EMPTY");
} else if(ts.size() == 1) {
int result = ts.first();
System.out.println(result + " " + result);
} else {
System.out.println(ts.pollLast() + " " + ts.pollFirst());
}
ts.clear();
}
}
}