题解 | 【模板】整数优先队列
【模板】整数优先队列
https://www.nowcoder.com/practice/a88e9711f7b04369982bbe8902278ae4
import sys
'''
python 提供了 heapq 模块,可以实现最小堆,完美匹配需求!
插入:heapq.heappush(heap, x) → O(log n)
查询最小值:heap[0] → O(1)
删除最小值:heapq.heappop(heap) → O(log n)
'''
import heapq
input = sys.stdin.read
data = input().split()
heap = []
n = int(data[0])
index = 1
for _ in range(n):
op = int(data[index]); index+=1
if op == 1:
x = int(data[index]); index+=1
heapq.heappush(heap,x)
elif op == 2:
print(heap[0])
elif op == 3:
if heap:
heapq.heappop(heap)
查看21道真题和解析