[백준] 11286번 절댓값 힙(python)

마뇽미뇽·5일 전
0

알고리즘 문제풀이

목록 보기
165/165

1. 문제

https://www.acmicpc.net/problem/11286

2. 풀이

(절댓값, 원래 값) 튜플 생성해서 heapq에 삽입할 각 숫자에 대해 (abs(숫자), 숫자) 형태의 튜플을 만들어 힙에 삽입한다

3. 코드

import heapq
import sys

n = int(sys.stdin.readline().rstrip())
heap = []

for _ in range(n):
    num = int(sys.stdin.readline().rstrip())
    if num != 0:
        heapq.heappush(heap, (abs(num), num))
    else:
        if len(heap) == 0:
            print(0)
        else:
            print(heapq.heappop(heap)[1])```
profile
Que sera, sera

0개의 댓글