티스토리 뷰

728x90

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

 

1927번: 최소 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0

www.acmicpc.net

 

파이썬의 heapq를 이용하면 쉽게 풀 수 있는 문제입니다.

 

heap에 data 입력 : heapq.heappush(자료구조, 데이터)

heap에서 data 출력 : heapq.heappop()

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import heapq
import sys
= int(sys.stdin.readline())
= []
for _ in range(N):
    x = int(sys.stdin.readline())
    if x == 0:
        if len(h) == 0:
            print(0)
        else:
            print(heapq.heappop(h))
    else:
        heapq.heappush(h, x)
cs

 

 

댓글