[알고리즘/백준]11279 : 최대 힙(python)

유현민·2022년 9월 10일
0

알고리즘

목록 보기
248/253

파이썬의 heapq를 이용했다.
다만 파이썬 heapq는 최소힙이기 때문에 (-, )이렇게 -값으로 붙여서 우선순위를 뒤집어 주었다.
pop을 할 때 [1]을 적어주면 최대값이 나온다.

import heapq
import sys

input = sys.stdin.readline
a = list()
l = 0
for _ in range(int(input())):
    n = int(input())
    if n == 0:
        if l == 0:
            print(0)
        else:
            l -= 1
            print(heapq.heappop(a)[1])
    heapq.heappush(a, (-n, n))
    l += 1
profile
smilegate megaport infra

0개의 댓글