Algorithm/Implementaion

[백준] 11286번 절대값 힙

등촌동 꼬북이 2020. 11. 26. 02:54

예전에 풀었던 우선순위큐를 이용하면 금방 풀 수 있는 문제

 

import sys
import heapq

heap = []
size = 0

for _ in range(int(sys.stdin.readline())):
    N = int(sys.stdin.readline())
    if N != 0:
        heapq.heappush(heap, (abs(N), N))
        size += 1
    else:
        if size == 0:
            print(0)
        else:
            print(heapq.heappop(heap)[1])
            size -= 1