[백준] 11286. 절댓값 힙

원숭2·2022년 1월 28일
0

백준

목록 보기
20/54

문제

풀이

  1. heapq 모듈에 tuple을 넣으면, tuple의 첫번째 값으로 연산을 함.
  2. 1.을 이용하여 tuple에 (절댒값, 원래 값)을 대입하여 heap 연산 수행함.

코드

import sys
import heapq

def solution() :
    n = int(sys.stdin.readline())
    heap = []
    
    for _ in range(n) :
        x = int(sys.stdin.readline())
        if x == 0 :
            if len(heap) == 0 :
                print(0)
            else :
                print(heapq.heappop(heap)[1])
        else :
            heapq.heappush(heap, (abs(x), x))

solution()

0개의 댓글