Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  5. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
  6. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  7. #define FILL(a,value) memset(a, value, sizeof(a))
  8.  
  9. #define SZ(a) (int)a.size()
  10. #define ALL(a) a.begin(), a.end()
  11. #define PB push_back
  12. #define MP make_pair
  13.  
  14. typedef long long LL;
  15. typedef vector<int> VI;
  16. typedef pair<int, int> PII;
  17.  
  18. const double PI = acos(-1.0);
  19. const int INF = 1000 * 1000 * 1000 + 7;
  20. const LL LINF = INF * (LL) INF;
  21.  
  22. const int MAX = 50500;
  23.  
  24. struct Point
  25. {
  26.     int x, y;
  27.     int ind;
  28.  
  29.     Point(int x, int y, int ind)
  30.     {
  31.         this->x = x;
  32.         this->y = y;
  33.         this->ind = ind;
  34.     }
  35.     Point(){}
  36.  
  37.     bool operator <(const Point& o)const
  38.     {
  39.         return y < o.y;
  40.     }
  41. };
  42.  
  43. bool cmpX(Point a, Point b)
  44. {
  45.     return MP(a.x, a.y) < MP(b.x, b.y);
  46. }
  47.  
  48. LL dist(Point a, Point b)
  49. {
  50.     LL res = (a.x - b.x) * (LL)(a.x - b.x) + (a.y - b.y) * (LL)(a.y - b.y);
  51.     return res;
  52. }
  53.  
  54. Point X[MAX];
  55. Point T[MAX];
  56. VI V;
  57. int n;
  58.  
  59. void merge(int L, int M, int R)
  60. {
  61.     memcpy(T + L, X + L, sizeof(Point) * (R - L + 1));
  62.  
  63.     int ind1 = L, ind2 = M + 1;
  64.     int ind = L;
  65.  
  66.     while(ind1 <= M || ind2 <= R)
  67.     {
  68.         if (ind1 > M)
  69.         {
  70.             X[ind++] = T[ind2++];
  71.             continue;
  72.         }
  73.         if (ind2 > R)
  74.         {
  75.             X[ind++] = T[ind1++];
  76.             continue;
  77.         }
  78.  
  79.         if (T[ind1] < T[ind2])
  80.         {
  81.             X[ind++] = T[ind1++];
  82.         }
  83.         else
  84.         {
  85.             X[ind++] = T[ind2++];
  86.         }
  87.     }
  88. }
  89.  
  90. pair<LL, PII> go(int L, int R)
  91. {
  92.     if (R - L + 1 < 4)
  93.     {
  94.         pair<LL, PII> res = MP(LINF, MP(INF, INF));
  95.  
  96.         FOR (i, L, R + 1)
  97.         {
  98.             FOR (j, i+1, R + 1)
  99.             {
  100.                 res = min(res, MP(dist(X[i], X[j]), MP(X[i].ind, X[j].ind)));
  101.             }
  102.         }
  103.  
  104.         sort(X+L, X+R+1);
  105.  
  106.         return res;
  107.     }
  108.  
  109.     int M = (L + R) / 2;
  110.     int midX = X[M].x;
  111.  
  112.     pair<LL, PII> res = min(go(L, M), go(M+1, R));
  113.  
  114.     merge(L, M, R);
  115.  
  116.     V.clear();
  117.     FOR (i, L, R + 1)
  118.     {
  119.         if ((X[i].x - midX) * (LL)(X[i].x - midX) <= res.first)
  120.         {
  121.             RFOR(j, SZ(V), 0)
  122.             {
  123.                 if ((X[i].y - X[V[j]].y) * (LL)(X[i].y - X[V[j]].y) > res.first) break;
  124.                 res = min(res, MP(dist(X[i], X[V[j]]), MP(X[i].ind, X[V[j]].ind)));
  125.             }
  126.  
  127.             V.PB(i);
  128.         }
  129.     }
  130.  
  131.     return res;
  132. }
  133.  
  134. int main()
  135. {
  136.     //freopen("in.txt", "r", stdin);
  137.     //ios::sync_with_stdio(false); cin.tie(0);
  138.  
  139.     scanf("%d", &n);
  140.     FOR (i, 0, n)
  141.     {
  142.         int x, y;
  143.         scanf("%d%d", &x, &y);
  144.         X[i] = Point(x, y, i);
  145.     }
  146.  
  147.     sort(X, X+n, cmpX);
  148.     pair<LL, PII> res = go(0, n-1);
  149.  
  150.     if (res.second.first > res.second.second) swap(res.second.first, res.second.second);
  151.  
  152.     printf("%d %d %.6f\n", res.second.first, res.second.second, sqrt(res.first));
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement