Advertisement
Alex_tz307

Cele mai apropiate puncte din plan

Jan 24th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define sqr(x) ((x) * (x))
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("cmap.in");
  8. ofstream fout("cmap.out");
  9.  
  10. class point {
  11.     public:
  12.         int x, y;
  13. };
  14.  
  15. int dist(const point &p1, const point &p2) {
  16.     return sqr(p1.x - p2.x) + sqr(p1.y - p2.y);
  17. }
  18.  
  19. int smallest_distance(const vector<point> &points, const vector<point> &points_by_y) {
  20.     int N = points.size();
  21.     if(N < 2)
  22.         return 9e18L;
  23.     vector<point> left = vector<point>(points.begin(), points.begin() + N / 2),
  24.                   right = vector<point>(points.begin() + N / 2, points.end());
  25.     vector<point> left_by_y, right_by_y;
  26.     for(const point &p : points_by_y)
  27.         if(make_pair(p.x, p.y) <= make_pair(left.back().x, left.back().y))
  28.             left_by_y.emplace_back(p);
  29.         else
  30.             right_by_y.emplace_back(p);
  31.     int d1 = smallest_distance(left, left_by_y),
  32.         d2 = smallest_distance(right, right_by_y),
  33.         d = min(d1, d2);
  34.     int middle_x = left.back().x;
  35.     vector<point> stripe;
  36.     for(const point &p : points_by_y)
  37.         if(sqr(p.x - middle_x) < d)
  38.             stripe.emplace_back(p);
  39.     int M = stripe.size();
  40.     for(int i = 0; i < M; ++i) {
  41.         // Lemma: Complexitatea celui de-al doilea for este O(1)
  42.         for(int j = i + 1; j < M && sqr(stripe[j].y - stripe[i].y) < d; ++j)
  43.             d = min(d, dist(stripe[i], stripe[j]));
  44.     }
  45.     return d;
  46. }
  47.  
  48. void test_case() {
  49.     int N;
  50.     fin >> N;
  51.     vector<point> points(N);
  52.     for(point &p : points)
  53.         fin >> p.x >> p.y;
  54.     sort(points.begin(), points.end(), [&](const point &A, const point &B) -> bool {
  55.         return make_pair(A.x, A.y) < make_pair(B.x, B.y);
  56.     });
  57.     vector<point> y_sorted = points;
  58.     sort(y_sorted.begin(), y_sorted.end(), [&](const point &A, const point &B) -> bool {
  59.         return A.y < B.y;
  60.     });
  61.     fout << fixed << setprecision(8) << sqrtl(smallest_distance(points, y_sorted)) << '\n';
  62. }
  63.  
  64. int32_t main() {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(nullptr);
  67.     cout.tie(nullptr);
  68.     int T = 1;
  69.     while(T--)
  70.         test_case();
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement