Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int INF = 92233720368547758;
  4. struct PointCordinates
  5. {
  6.     int x,y;
  7. };
  8. int SortConstructorX (PointCordinates First, PointCordinates Second)
  9. {
  10.     if(First.x < Second.x) return 1;
  11.     if(First.x == Second.x && First.y < Second.y) return 1;
  12.     return 0;
  13. }
  14. int SortConstructorY (PointCordinates First, PointCordinates Second)
  15. {
  16.     if(First.y < Second.y) return 1;
  17.     if(First.y == Second.y && First.x < Second.x) return 1;
  18.     return 0;
  19. }
  20. long long FindShortestDistance(int _1st, int _2nd, vector<PointCordinates> Field )
  21. {
  22.     if(_1st==_2nd) return INF;
  23.     int Mid =(_1st+_2nd)/2;
  24.     long long MinResult=min(FindShortestDistance(_1st,Mid,Field),FindShortestDistance(Mid+1,_2nd,Field));
  25.     vector<PointCordinates>SortedYs;
  26.     for(int i=_1st; i<_2nd+1; i++)
  27.     {
  28.         if((Field[i].x-Field[Mid].x)*(Field[i].x-Field[Mid].x)<=MinResult)
  29.             SortedYs.push_back(Field[i]);
  30.     }
  31.     sort(SortedYs.begin(),SortedYs.end(),SortConstructorY);
  32.     for(int i=0; i<SortedYs.size(); i++)
  33.         for(int n=i+1; n<i+9 && n<SortedYs.size(); n++)
  34.         {
  35.             int XsDiff=SortedYs[n].x-SortedYs[i].x;
  36.             int YsDiff=SortedYs[n].y-SortedYs[i].y;
  37.             long long X = XsDiff * XsDiff;
  38.             long long Y = YsDiff * YsDiff;
  39.             MinResult=min(MinResult,X+Y);
  40.         }
  41.     return MinResult;
  42. }
  43. int main()
  44. {
  45.     int Sets;
  46.     cin >> Sets;
  47.     while(Sets--)
  48.     {
  49.         int NoOfPoints;
  50.         cin >> NoOfPoints;
  51.         vector <PointCordinates> Field(NoOfPoints);
  52.         for(int i=0; i<NoOfPoints; i++) cin >> Field[i].x >> Field[i].y;
  53.         sort(Field.begin(),Field.end(), SortConstructorX);
  54.         long long Result = FindShortestDistance(0,Field.size()-1,Field);
  55.         cout << Result <<endl;
  56.     }
  57. }
  58. XDDDDDDDDDDDDDDDDDDDD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement