HackerRank

More on data structure.

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <limits>
#include <queue>
using namespace std;

typedef pair<int, int> Point;

struct DistComp
{
    bool operator()(const Point &p1, const Point &p2)
    {
        long long x1 = p1.first;
        long long y1 = p1.second;
        long long x2 = p2.first;
        long long y2 = p2.second;

        return (x1 * x1 + y1 * y1) > (x2 * x2 + y2 * y2);
    }
};

int main() 
{
    int n; cin >> n;
    vector<Point> in;
    while (n--)
    {
        int x, y; cin >> x >> y;
        in.push_back(Point(x, y));
    }

    map<float, priority_queue<Point, vector<Point>, DistComp>> rec1;
    map<float, priority_queue<Point, vector<Point>, DistComp>> rec2;

    for (auto &p : in)
    {
        int x = p.first;
        int y = p.second;

        float ret = atan2(y, x);

        if (ret >= 0)
        {
            rec1[ret].push(Point(x, y));
        }
        else
        {
            rec2[ret].push(Point(x, y));
        }
    }

    for (auto it = rec1.begin(); it != rec1.end(); it++)
    {
        auto &m = it->second;
        while (!m.empty())
        {
            auto p = m.top(); m.pop();
            cout << p.first << " " << p.second << endl;
        }
    }
    for (auto it = rec2.begin(); it != rec2.end(); it++)
    {
        auto &m = it->second;
        while (!m.empty())
        {
            auto p = m.top(); m.pop();
            cout << p.first << " " << p.second << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tonix/p/4562576.html