https://www.acmicpc.net/problem/11286
import heapq
import sys
input=sys.stdin.readline
N=int(input())
heap=[]
for _ in range(N):
x=int(input())
if x==0: #출력
if not heap: #비어있다면
print(0)
else:
print(heapq.heappop(heap)[1])
else:
#(abs(x), x)를 힙에 넣음으로써 절댓값 기준 정렬
# ex) -1을 heap에 넣으면 (1,-1)로 넣어짐
heapq.heappush(heap,(abs(x),x))
2025.05.22 - [coding test/etc] - [Python] heapq 라이브러리
[Python] heapq 라이브러리
파이썬에는 heapq 라는 라이브러리가 있다.import heapqheapq는 파이썬의 내장 모듈로, 힙(heap) 자료구조를 쉽게 사용할 수 있게 해준다.heapq 라이브러리 사용하면 우선순위 큐(priority queue)도 쉽게 구현
wish404.tistory.com
'coding test > Baekjoon' 카테고리의 다른 글
| [Python] 1715번 카드 정렬하기 (2) | 2025.08.30 |
|---|---|
| [Python] 16236번 아기 상어 (1) | 2025.07.01 |
| [Python] 1543번 문서 검색 (0) | 2025.05.22 |
| [Python] 1106번 호텔 (1) | 2025.05.19 |
| [Python] 2293번 동전 1 (0) | 2025.05.19 |
