ETC/Algorithm

백준, 최소 힙, 1927

coding_genie 2024. 8. 22. 00:07

[유형]

자료구조

 

[문제링크]

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

 

[요약]

정수 x가 주어졌을 때, 

x=0이면, 배열에서 가장 작은 값을 출력하고 그 값을 배열에서 제거한다. 만약, 배열이 비어있는 경우라면 0을 출력한다.

x=자연수이면, 배열에 자연수 x를 넣는다.

 

[문제풀이]

heap을 사용한다.

import sys
def input():
    return sys.stdin.readline().rstrip()

import heapq

n = int(input())
heap =[]
for _ in range(n):
    x = int(input())
    if x == 0:
        if not heap:
            print(0)
        else:
            print(heapq.heappop(heap))
    else:
        heapq.heappush(heap, x)