测评地址: https://www.luogu.org/problemnew/show/P3378
通过时间: 2019.5.14
评测详情: https://www.luogu.org/recordnew/show/19039840
用时: 2837ms / 内存: 796KB
优先队列本质上是一个堆实现的。
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> > a;
int main()
{
int n,i,cmd,x;
cin>>n;
for(i=1;i<=n;i++){
cin>>cmd;
if(cmd==1){
cin>>x;
a.push(x);
}
else if(cmd==2)
cout<<a.top()<<endl;
else
a.pop();
}
return 0;
}
用scanf()
和printf()
可以更快,但由于众所周知的原因…
优先队列基本操作:
- top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素 (并排序)
pop 弹出队头元素
swap 交换内容
定义:
//升序队列(队头最小)
priority_queue <int,vector<int>,greater<int> >q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;
//与 priority_queue <int> a 相同 ^ 这里要有空格!