본문 바로가기
백준

최소 힙 - 1927번

by 청원뿔세포 2022. 10. 31.

파이썬의 모듈 heapq는 중복을 허용하지 않고, 작은 값을 root에 두도록 정렬하는 최소힙구조이며, pop을 할 경우 root의 값이 없어지면서 반환된다.

import sys
import heapq

t = int(input())
h = []
for _ in range(t):
  inn = int(sys.stdin.readline())
  if not inn and h:
    r = heapq.heappop(h)
    print(r)
  elif not inn and not h:
    print(0)
  else:
    heapq.heappush(h,inn)

'백준' 카테고리의 다른 글

후위 표기식 - 1918번  (0) 2022.10.31
최대 힙 - 11279번  (0) 2022.10.31
덩치 - 7568번  (0) 2022.10.31
최대공약수와 최소공배수 - 2609번  (0) 2022.10.30
부녀회장이 될테야 - 2775번  (0) 2022.10.26

댓글