题解 | 两端问优先队列

两端问优先队列

https://www.nowcoder.com/practice/da2887a3fd8549ad826c9cbdaa67f513

import heapq
from collections import Counter
n=int(input())
s=[]
ms=[]
count=Counter()
for i in range(n):
    op=list(map(int,input().split()))
    if op[0]==1:
        if count[op[1]]==0:
            heapq.heappush(s,op[1])
            heapq.heappush(ms,-op[1]) 
        count[op[1]]+=1   
    if op[0]==2:
        while s and count[s[0]]==0:
            heapq.heappop(s)
        print(s[0])
    if op[0]==3:
        while ms and count[-ms[0]]==0:
            heapq.heappop(ms)
        print(-ms[0])
    if op[0]==4:
        while s and count[s[0]]==0:
            heapq.heappop(s)
        count[s[0]]-=1    
    if op[0]==5:
        while s and count[-ms[0]]==0:
            heapq.heappop(ms)
        count[-ms[0]]-=1
        
        




全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务