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\n",heap[0]);
        }
        else{
            pop_heap(heap,heap+cnt,greater<int>());
            cnt--;
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2021-12-14
  • 2021-12-25
  • 2021-08-05
猜你喜欢
  • 2021-12-16
  • 2022-01-08
  • 2022-01-08
  • 2022-12-23
  • 2021-08-04
相关资源
相似解决方案