Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. struct Point {
  7.     int x;
  8.     int y;
  9.     int distance = sqrt(x * x + y * y);
  10.     Point() : x(0), y(0) {}
  11.     Point(int a, int b) : x(a), y(b) {}
  12. };
  13.  
  14. struct CustomCompare
  15. {
  16.     bool operator()(const Point lhs, const Point rhs)
  17.     {
  18.         if (lhs.distance == rhs.distance)
  19.         {
  20.             if (lhs.x == rhs.x)
  21.             {
  22.                 return lhs.y > rhs.y;
  23.             }
  24.             else
  25.             {
  26.                 return lhs.x > rhs.x;
  27.             }
  28.         }
  29.         return lhs.distance > rhs.distance;
  30.     }
  31. };
  32.  
  33. int main()
  34. {
  35.     int countAll, countClosest;
  36.     cin >> countAll >> countClosest;
  37.  
  38.     priority_queue <Point, vector<Point>, CustomCompare> pq;
  39.  
  40.     for (size_t i = 0; i < countAll; i++)
  41.     {
  42.         int x, y;
  43.         cin >> x >> y;
  44.  
  45.         Point p(x, y);
  46.  
  47.         pq.push(p);
  48.     }
  49.  
  50.     for (size_t i = 0; i < countClosest; i++)
  51.     {
  52.         cout << pq.top().x << " " << pq.top().y << endl;
  53.         pq.pop();
  54.     }
  55.  
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement