zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Hbase数据库简介
    v8引擎详解
    TCP、UDP和HTTP详解
    关于websocket的代码,实现发送信息和监听信息(前端 后端(node.js))
    浏览器的渲染流程
    事件的各个阶段
    回流和重绘
    Nginx入门教程
    解决浏览器跨域
    TCP三次握手
  • 原文地址:https://www.cnblogs.com/tonix/p/4562576.html
Copyright © 2011-2022 走看看