洛谷p3378 堆--基于优先队列实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/imotolove/article/details/80917813
加入了读入挂,如果去掉greater<int>,则为最大堆
#include<cstdio>
#include<queue> //不要忘记头文件
using namespace std;
priority_queue<int,vector<int>,greater<int> > q; //定义优先队列,升序 
int n,a,b;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;

}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        a=read();
        if(a==1) //根据题目要求来
        {
            b=read();
            q.push(b); //把b压入优先队列
        }
        if(a==2)
        {
            int ans=q.top(); //获取现在优先级最高的元素的值
            printf("%d\n",ans); //输出答案
        }
        if(a==3)
            q.pop(); //将优先级最高的元素弹出
    }
}

猜你喜欢

转载自blog.csdn.net/imotolove/article/details/80917813