P3378 【模板】堆 题解

· · 个人记录

题目传送门
可以用优先队列来解决
《论c++有stl的好处》

#include <bits/stdc++.h>
using namespace std;
int main() {
    priority_queue< int,vector<int>,greater<int> >a; ////定义优先队列,升序排列(降序可以用less)
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        int x;
        cin>>x;
        if(x==1) {
            int y;
            cin>>y;
            a.push(y);
        }
        if(x==2)
            cout<<a.top()<<"\n"; //输出队首
        if(x==3)
            a.pop(); //删除队首
    }
    return 0;
}