1927: 최소 힙
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
print(heapq.heappop(heap))
else:
heapq.heappush(heap, cmd)
- python의 heapq 묘듈을 사용하여 구현
11279: 최대 힙
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
print(-heapq.heappop(heap))
else:
heapq.heappush(heap, -cmd)
11286: 절댓값 힙
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
tmp = heapq.heappop(heap)
print(tmp[1])
else:
heapq.heappush(heap, [abs(cmd), cmd])