# 优先队列回顾2## 题目描述输入 n 组数据每组数据包括两个整数 x 和 y把这组数据进行排序排序规则是:先按照 y 从小到大排当 y 一样时按照 x 从小到大排。注:这道题要用 pair 类型的优先队列来完成。## 输入格式首先输入 n0 n ≤ 100代表数据的组数。接着输入 n 组数据每组数据包括两个整数 x 和 y0 x y ≤ 100之间用一个空格隔开。## 输出格式把排序好的
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;//定义优先队列
int n,x,y;
cin>>n;
while(n--)
{
cin>>x>>y;
q.push(make_pair(y,x));
}
while(q.size())
{
auto t=q.top();
cout<<t.second<<" "<<t.first<<endl;//输出时要注意按照要求交换x和y的位置
q.pop();
}
return 0;
}
原文地址: http://www.cveoy.top/t/topic/bPtj 著作权归作者所有。请勿转载和采集!