STL之priority_queue3

描述

使用STL中的优先队列,将n个点按照横坐标从小到大顺序排序,如果横坐标相同,按照纵坐标从小到大排序。

部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

 

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        Input();
        while(!qu.empty())
        {
            Point p = qu.top();
            cout<<p.x<<" "<<p.y<<endl;
            qu.pop();
        }
    }
    return 0;
}

输入

输入数据有多组,第一行为t,接下来有t组数据。

每组的第一行为正整数n,接下来有n行,每行一个点,包含横坐标和纵坐标,均为整数。

输出

每组输出排序后的所有点,每行一个点。

样例输入

 2
3
3 2
2 3
4 1
4
1 2
3 3
1 1
3 2

样例输出

2 3
3 2
4 1
1 1
1 2
3 2
3 3

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <deque>
using namespace std;
typedef  struct Point {
    int x;
    int y;
    bool operator>(const Point &p) const
    {
        if(x!=p.x)
        return x>p.x;
        return y>p.y;
    }
}Point;
priority_queue< Point,vector<Point>,greater<Point> > qu;
void Input()
{
    Point p;
    int n;
    cin>>n;
    while(n--)
    {
        cin>>p.x>>p.y;
        qu.push(p);
    }    
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        Input();
        while(!qu.empty())
        {
            Point p = qu.top();
            cout<<p.x<<" "<<p.y<<endl;
            qu.pop();
        }
    }
    return 0;
}

 

 

posted @ 2018-04-05 20:11  极限之旅  阅读(326)  评论(0编辑  收藏  举报