카테고리 없음

백준 1927번 최소 힙

blablax5 2024. 1. 18. 09:35

최대 힙의 반대로 입력 ,출력을 하면 된다.

import heapq
import sys

heap = []
num_count = int(sys.stdin.readline())
for _ in range(num_count):
    num = int(sys.stdin.readline())
    # 힙이 없는데 출력 할 경우
    if not heap and num == 0:
        print(0)
        continue

    # 0 이상 이면 값을 추가
    if num > 0:
        heapq.heappush(heap, num)

    # 힙이 있으면 출력
    if heap and num == 0:
        print(heapq.heappop(heap))