PS/BOJ

[BOJ] 최소 힙

RUBY_루비 2020. 6. 24. 16:33

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

 

1. 문제 이해 및 해결과정

https://hiruby.tistory.com/178

 

2. 풀이방법

import sys
import heapq as hq
sys.stdin = open("input.txt","r")
a=[]
n = int(input())
for _ in range(n):
    x=int(input())
    if x==-1: #종료
        break
    elif x==0: #출력
        if len(a)==0:
            print(0)
        else:
            print(hq.heappop(a))
    else: #넣기
        hq.heappush(a,x) #a리스트에 x넣음

 

3. 오답원인

-input() 으로 코드를 제출할 시 시간초과가 뜨지만, sys.stdin.readline() 으로 제출할 시 정답이 된다.

 

4. 알게된 점