优先队列

 
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int,int> pll;
int main()
{
    priority_queue<pll,vector<pll>,less<pll> > q; //降序 ,可写成priority_queue<pll> q;
    priority_queue<pll,vector<pll>,greater<pll> > p; //升序 
    int n,a,b;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b;
        q.push({a,b}); //两种输入方式
        p.push(pll(a,b)); //都可以
    }
    while(!q.empty())
    {
        pll w=q.top();
        q.pop();
        cout<<w.first<<" "<<w.second<<endl;
    }
    while(!p.empty())
    {
        pll w=p.top();
        cout<<w.first<<" "<<w.second<<endl;
        p.pop();
    }
    return 0;
}

 

posted @ 2022-05-09 17:22  xxj112  阅读(27)  评论(0)    收藏  举报