堆【STL—优先队列】
需要用到的头文件
#include<queue>
定义一个堆
priority_queue<类型>name;
P.S. 定义的堆默认为大根堆,要定义小根堆可以
priority_queue<int,vector<类型>,greater<类型> >name;
堆的基本操作
q.push(x);
q.empty();
q.top()
q.pop()
q.size()
模版的代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
priority_queue<int,vector<int>,greater<int> >heap;
int n,k,z,num;
int main()
{
scanf("%d",&n);
while(n --)
{
scanf("%d",&k);
if(k == 1)
{
scanf("%d",&num);
heap.push(num);
}
if(k == 2)
{
num = heap.top();
printf("%d\n",num);
}
if(k == 3)
{
heap.pop();
}
}
return 0;
}