백준 11279 최대 힙 (python) 🥈실버 2티어
2023. 3. 20. 09:05ㆍ카테고리 없음
728x90
https://www.acmicpc.net/problem/11279
11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
import heapq
import sys
read = sys.stdin.readline
n = int(input())
heap = []
result = []
for _ in range(n):
x = int(read())
if x == 0: #입력값 x가 0이 들어왔을 때
if len(heap) == 0: #heap이 비어있으면 결과에 0추가
result.append(0)
else: #힙에서 최솟값을 추출한 뒤 결과리스트에 추가
result.append(heapq.heappop(heap))
else: #0이 아닌 x가 입력되면
heapq.heappush(heap, -x) #힙에 x를 추가
for item in result:
print(-item)