Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int INF = 92233720368547758;
- struct PointCordinates
- {
- int x,y;
- };
- int SortConstructorX (PointCordinates First, PointCordinates Second)
- {
- if(First.x < Second.x) return 1;
- if(First.x == Second.x && First.y < Second.y) return 1;
- return 0;
- }
- int SortConstructorY (PointCordinates First, PointCordinates Second)
- {
- if(First.y < Second.y) return 1;
- if(First.y == Second.y && First.x < Second.x) return 1;
- return 0;
- }
- long long FindShortestDistance(int _1st, int _2nd, vector<PointCordinates> Field )
- {
- if(_1st==_2nd) return INF;
- int Mid =(_1st+_2nd)/2;
- long long MinResult=min(FindShortestDistance(_1st,Mid,Field),FindShortestDistance(Mid+1,_2nd,Field));
- vector<PointCordinates>SortedYs;
- for(int i=_1st; i<_2nd+1; i++)
- {
- if((Field[i].x-Field[Mid].x)*(Field[i].x-Field[Mid].x)<=MinResult)
- SortedYs.push_back(Field[i]);
- }
- sort(SortedYs.begin(),SortedYs.end(),SortConstructorY);
- for(int i=0; i<SortedYs.size(); i++)
- for(int n=i+1; n<i+9 && n<SortedYs.size(); n++)
- {
- int XsDiff=SortedYs[n].x-SortedYs[i].x;
- int YsDiff=SortedYs[n].y-SortedYs[i].y;
- long long X = XsDiff * XsDiff;
- long long Y = YsDiff * YsDiff;
- MinResult=min(MinResult,X+Y);
- }
- return MinResult;
- }
- int main()
- {
- int Sets;
- cin >> Sets;
- while(Sets--)
- {
- int NoOfPoints;
- cin >> NoOfPoints;
- vector <PointCordinates> Field(NoOfPoints);
- for(int i=0; i<NoOfPoints; i++) cin >> Field[i].x >> Field[i].y;
- sort(Field.begin(),Field.end(), SortConstructorX);
- long long Result = FindShortestDistance(0,Field.size()-1,Field);
- cout << Result <<endl;
- }
- }
- XDDDDDDDDDDDDDDDDDDDD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement