-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7662.py
49 lines (45 loc) · 1.53 KB
/
7662.py
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
# 이중 우선순위 큐
from sys import stdin
import heapq
from collections import defaultdict
T = int(stdin.readline().rstrip())
for _ in range(T):
k = int(stdin.readline().rstrip())
max_heap = []
min_heap = []
cnt_nums = 0
min_check = defaultdict(int)
max_check = defaultdict(int)
for __ in range(k):
ord, num = stdin.readline().rstrip().split()
if ord == "I":
heapq.heappush(min_heap, int(num))
heapq.heappush(max_heap, -int(num))
cnt_nums += 1
else:
if cnt_nums > 0:
cnt_nums -= 1
if num == "-1":
pop_num = heapq.heappop(min_heap)
while max_check[pop_num]:
max_check[pop_num] -= 1
pop_num = heapq.heappop(min_heap)
min_check[pop_num] += 1
else:
pop_num = heapq.heappop(max_heap)
while min_check[-pop_num]:
min_check[-pop_num] -= 1
pop_num = heapq.heappop(max_heap)
max_check[-pop_num] += 1
if cnt_nums:
min_num = heapq.heappop(min_heap)
while max_check[min_num]:
max_check[min_num] -= 1
min_num = heapq.heappop(min_heap)
max_num = -heapq.heappop(max_heap)
while min_check[max_num]:
min_check[max_num] -= 1
max_num = -heapq.heappop(max_heap)
print(max_num, min_num)
else:
print("EMPTY")