문제
이중 우선순위 큐(dual priority queue)는 전형적인 우선순위 큐처럼 데이터를 삽입, 삭제할 수 있는 자료 구조이다. 전형적인 큐와의 차이점은 데이터를 삭제할 때 연산(operation) 명령에 따라 우선순위가 가장 높은 데이터 또는 가장 낮은 데이터 중 하나를 삭제하는 점이다. 이중 우선순위 큐를 위해선 두 가지 연산이 사용되는데, 하나는 데이터를 삽입하는 연산이고 다른 하나는 데이터를 삭제하는 연산이다. 데이터를 삭제하는 연산은 또 두 가지로 구분되는데 하나는 우선순위가 가장 높은 것을 삭제하기 위한 것이고 다른 하나는 우선순위가 가장 낮은 것을 삭제하기 위한 것이다.
정수만 저장하는 이중 우선순위 큐 Q가 있다고 가정하자. Q에 저장된 각 정수의 값 자체를 우선순위라고 간주하자.
Q에 적용될 일련의 연산이 주어질 때 이를 처리한 후 최종적으로 Q에 저장된 데이터 중 최댓값과 최솟값을 출력하는 프로그램을 작성하라.
출력
출력은 표준출력을 사용한다. 각 테스트 데이터에 대해, 모든 연산을 처리한 후 Q에 남아 있는 값 중 최댓값과 최솟값을 출력하라. 두 값은 한 줄에 출력하되 하나의 공백으로 구분하라. 만약 Q가 비어있다면 ‘EMPTY’를 출력하라.
W3sicHJvYmxlbV9pZCI6Ijc2NjIiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWM3NzRcdWM5MTEgXHVjNmIwXHVjMTIwXHVjMjFjXHVjNzA0IFx1ZDA1MCIsImRlc2NyaXB0aW9uIjoiPHA+XHVjNzc0XHVjOTExIFx1YzZiMFx1YzEyMFx1YzIxY1x1YzcwNCBcdWQwNTAoZHVhbCBwcmlvcml0eSBxdWV1ZSlcdWIyOTQgXHVjODA0XHVkNjE1XHVjODAxXHVjNzc4IFx1YzZiMFx1YzEyMFx1YzIxY1x1YzcwNCBcdWQwNTBcdWNjOThcdWI3ZmMgXHViMzcwXHVjNzc0XHVkMTMwXHViOTdjIFx1YzBiZFx1Yzc4NSwgXHVjMGFkXHVjODFjXHVkNTY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ZSB0d28gb3IgbW9yZSBtYXhpbWEgKG1pbmltYSkgaW4gUSB3aGVuIHRoZSBvcGVyYXRpb24gaXMgJmxzcXVvO0QgMSZyc3F1bzsoJmxzcXVvO0QgLTEmcnNxdW87KSwgb25seSBvbmUgb2YgdGhlbSBpcyBkZWxldGVkLiZuYnNwOzxcL3A+XHJcblxyXG48cD5JZiBRIGlzIGVtcHR5IHdoZW4gdGhlIG9wZXJhdGlvbiBpcyAmbHNxdW87RCZyc3F1bzsgeW91IGNhbiBpZ25vcmUgaXQuIEVhY2ggaW5wdXQgdG8gYmUgaW5zZXJ0ZWQgaW50byBRIGlzIGEgMzItYml0IGludGVnZXIuJm5ic3A7PFwvcD5cclxuIiwib3V0cHV0IjoiPHA+WW91ciBwcm9ncmFtIGlzIHRvIHdyaXRlIHRvIHN0YW5kYXJkIG91dHB1dC4gRm9yIGVhY2ggdGVzdCBjYXNlLCBwcmludCB0aGUgbWF4aW11bSBhbmQgdGhlIG1pbmltdW0gbnVtYmVycyBhbW9uZyB0aG9zZSB3aGljaCByZW1haW4gaW4gUS4gVGhlIHR3byBudW1iZXJzIHNob3VsZCBiZSBwcmludGVkIGluIGEgbGluZSBzZXBhcmF0ZWQgYnkgYSBibGFuay4gSWYgUSBpcyBlbXB0eSBhdCB0aGUgZW5kLCBwcmludCAmbHNxdW87RU1QVFkmcnNxdW87LiZuYnNwOzxcL3A+XHJcbiIsImhpbnQiOiIiLCJvcmlnaW5hbCI6IjEiLCJodG1sX3RpdGxlIjoiMCIsInByb2JsZW1fbGFuZ190Y29kZSI6IkVuZ2xpc2gifV0=
접근 방법
- 딕셔너리를 통해 현재 힙에 포함되어있는 값은 키, 해당 값의 개수는 밸류로 구현한다.
- 이후 우선순위 큐를 최소 힙, 최대 힙을 통해 구현하고, 값이 생기면 두 힙 모두에 저장하고, 딕셔너리에 키와 값을 추가하거나 값을 하나 증가시켜준다.
- 만약 최솟값이나 최대 값을 삭제하는 경우 딕셔너리에 해당 값이 존재하는지 확인하고, 없다면 해당 값을 그냥 빼준 뒤, 다시 최대값 또는 최솟값을 뺀다.
- 딕셔너리에 해당 값이 존재하는 지에 대한 확인은 set을 통해 진행한다.
코드
# https://www.acmicpc.net/problem/7662
# 접근 방법
# 딕셔너리를 통해 현재 힙에 포함되어있는 값은 키, 해당 값의 개수는 밸류로 구현한다.
# 이후 우선순위 큐를 최소 힙, 최대 힙을 통해 구현하고, 값이 생기면 두 힙 모두에 저장하고, 딕셔너리에 키와 값을 추가하거나 값을 하나 증가시켜준다.
# 만약 최솟값이나 최대 값을 삭제하는 경우 딕셔너리에 해당 값이 존재하는지 확인하고, 없다면 해당 값을 그냥 빼준 뒤, 다시 최대값 또는 최솟값을 뺀다.
# 딕셔너리에 해당 값이 존재하는 지에 대한 확인은 set을 통해 진행한다.
import sys, heapq, math
input = sys.stdin.readline
tc = int(input())
for _ in range(tc):
k = int(input())
min_h, max_h = [], []
element_dict = {}
check_value = set()
for _ in range(k):
order = input().split()
if order[0] == 'D' and order[1] == '-1':
while min_h:
if min_h[0] in check_value:
x = heapq.heappop(min_h)
element_dict[x] -= 1
if not element_dict[x]:
del element_dict[x]
check_value.remove(x)
break
heapq.heappop(min_h)
elif order[0] == "D":
while max_h:
if -max_h[0] in check_value:
x = -heapq.heappop(max_h)
element_dict[x] -= 1
if not element_dict[x]:
del element_dict[x]
check_value.remove(x)
break
heapq.heappop(max_h)
else:
x = int(order[1])
if x not in check_value:
element_dict[x] = 0
check_value.add(x)
heapq.heappush(min_h, x)
heapq.heappush(max_h, -x)
element_dict[x] += 1
if len(check_value) == 0:
print("EMPTY")
else:
min_v = math.inf
max_v = -math.inf
for x in check_value:
min_v = min(min_v, x)
max_v = max(max_v, x)
print(max_v, min_v, sep = " ")