본문 바로가기
Algorithm/Implementaion

[백준] 11286번 절대값 힙

by 등촌동 꼬북이 2020. 11. 26.

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

 

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

'Algorithm > Implementaion' 카테고리의 다른 글

[백준] 1357 뒤집힌 덧셈  (0) 2020.11.30
[백준] 1417번 국회의원 선거  (0) 2020.11.28
[백준] 1913번 달팽이  (0) 2020.11.26
[백준] 1032번 명령 프롬프트  (0) 2020.11.25
[백준] 1966번 프린터 큐  (0) 2020.11.25

댓글