「优先队列实现」洛谷 P3378 【模板】堆

测评地址: 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 相同          ^ 这里要有空格!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值