zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    又见回文 字符串
    学密码学一定得学程序 KMP
    学密码学一定得学程序 KMP
    KMP简单应用
    KMP简单应用
    用ALAssetsLibrary将过滤后图片写入照片库
    iOS 8版本信息与屏幕尺寸
    屏幕适配的那些坑
    从工程中删除Cocoapods
    AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸
  • 原文地址:https://www.cnblogs.com/andrew3/p/8724123.html
Copyright © 2011-2022 走看看