[백준/파이썬] 1927 최소 힙

bye9·2021년 1월 29일
0

알고리즘(코테)

목록 보기
37/130

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


알고리즘 분류

  • 우선순위큐

문제풀이

최소힙구현을 위한 heapq모듈을 사용했다.

x가 0이지만 힙이 비어있는 경우 0을 출력하고
힙이 비어있지 않다면 최소값을 출력하고 제거한다.
그냥 자연수라면 힙에 삽입한다.

소스코드

import heapq
import sys
input=sys.stdin.readline

n=int(input())
exp=[]
for _ in range(n):
  exp.append(int(input()))

heap=[]
heapq.heapify(heap)
for i in exp:
  if i==0 and len(heap)==0:
    print(0)
  elif i==0:
    print(heapq.heappop(heap))
  else:
    heapq.heappush(heap,i)

0개의 댓글