File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.InputStreamReader ;
4+ import java.util.StringTokenizer ;
5+ import java.util.TreeMap ;
6+
7+ public class Main {
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ int T = Integer . parseInt(br. readLine());
11+
12+ for (int t = 0 ; t< T ; t++ ){
13+ int K = Integer . parseInt(br. readLine());
14+ TreeMap<Integer , Integer > map = new TreeMap<> ();
15+
16+ for (int i = 0 ; i < K ; i++ ) {
17+ StringTokenizer st = new StringTokenizer (br. readLine());
18+ String command = st. nextToken();
19+ int value = Integer . parseInt(st. nextToken());
20+
21+ if (command. equals(" I" )) {
22+ map. put(value, map. getOrDefault(value, 0 ) + 1 );
23+ } else if (command. equals(" D" )) {
24+ if (map. isEmpty())
25+ continue ;
26+ int key = (value == 1 ) ? map. lastKey() : map. firstKey();
27+ int count = map. get(key);
28+ if (count == 1 ) {
29+ map. remove(key);
30+ } else {
31+ map. put(key, count - 1 );
32+ }
33+ }
34+ }
35+ if (map. isEmpty()) {
36+ System . out. println(" EMPTY" );
37+ } else {
38+ System . out. println(map. lastKey() + " " + map. firstKey());
39+ }
40+ }
41+ }
42+ }
43+ ```
You can’t perform that action at this time.
0 commit comments