heap,也就是堆,作为常用的优先队列的替代品,其实还是有优越性的。
make_heap(v.first(),v.end(),cmp()) 把容器调整成堆。
push_heap(v.first(),v.end(),cmp()) 在容器本身是堆,往容器的end中插入后,把end后移,再调用这个。
pop_heap(v.first(),v.end(),cmp()) 在容器本身是堆,先调用这个,再把end删除,最后把end前移。
速度大概是priority_queue的两倍。
#include<bits/stdc++.h> using namespace std; #define ll long long int heap[1000005]; int cnt=0; int main(){ int n; scanf("%d",&n); while(n--){ int ins; scanf("%d",&ins); if(ins==1){ scanf("%d",&heap[cnt++]); push_heap(heap,heap+cnt,greater<int>()); } else if(ins==2){ printf("%d ",heap[0]); } else{ pop_heap(heap,heap+cnt,greater<int>()); cnt--; } } }