Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
}
posted on 2015-06-09 09:34  Tonix  阅读(251)  评论(0编辑  收藏  举报