널리 잘 알려진 자료구조 중 최소 힙이 있다. 최소 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.
0 -> pop
자연수 -> push
입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 작은 값을 출력하라고 한 경우에는 0을 출력하면 된다.
from heapq import heappush, heappop
n = int(input()) # 연산의 개수
heap = []
output = []
for _ in range(n):
x = int(input())
if x == 0:
if heap: output.append(heappop(heap))
else: output.append(0)
else: heappush(heap, x)
for i in output:
print(i)
파이썬의 heap(최소힙)을 그대로 사용하면 된다.