First of all thanks to Bole's comments, has been modified ~ ~
Problem Description:
Given the horizontal ordinate of n points, ask for their nearest distance.
Cases
2
1 0
0 1
Output 1.4142
Problem Analysis:
If the direct violence is to be obtained, the complexity is O (n^2), from the N point to select 2 points, N (n-1)/2
And if the partition method, the complexity can be reduced to O (Nlogn), the method is based on horizontal ordinate from small to large sort
Then look for Left~mid (point all on the left), Mid+1~right (Point all on the right) of the minimum point pair, and then the minimum of two of them and a point on the left side, a point on the right side of the situation compared (a bit like the division of the maximum subsequence and the sequence), regardless of the middle of the treatment, What's the status of recursion, if Left==right is only a point, return infinity, if the left+1==right description has two points, direct return distance
Again, consider the middle of the situation (a point on the left, a point on the right), considering that the shortest distance has been found before D, so the middle point pair is already in the distance 2d*2d square (this is very important. )
Strictly speaking, it should be round but the circle calculation is too troublesome and has a precision problem.
Find these points and combine them all to get their shortest distance, and update D if it's smaller than d.
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std;
int n;
struct node {double x;
Double y;
};
int CMP (node A,node b)//two points in accordance with the horizontal coordinates from small to large sort, the same as the ordinate from small to large {if (a.x==b.x) return a.y<b.y;
Return a.x<b.x;
Node point[10005];
int cmp2 (int a,int b) {return point[a].y<point[b].y;
Double distance (double c1,double c2,double d1,double D2) {return sqrt ((C1-D1) * (C1-D1) + (C2-D2) * (C2-D2));
The double calculate (int left,int right)//left is the leftmost coordinate, and right-hand is the rightmost coordinate {int i,j,mid,tempnumber;
Double d,d1,d2;
int temp[10005]; if (left==right)//If is a point, then the distance is naturally 0 return 999999;//returns an infinity if (left+1==right)//If it is two points, return directly to two points
Distance (POINT[LEFT].X,POINT[LEFT].Y,POINT[RIGHT].X,POINT[RIGHT].Y);
Mid= (left+right) >>1; D1=calculate (Left,mid);//The nearest point pair of the left part d2=calculate (mid+1,right);//The nearest point pair of the right part d=d1<d2?d1:d2;//take smaller, then consider a point on the leftPart, a point in the right part of the case i=mid;
tempnumber=0;
while (I>=0&&POINT[MID].X-POINT[I].X<D)//from point to left to find {temp[tempnumber++]=i;
i--;
} i=mid+1; while (I<N&&POINT[I].X-POINT[MID].X<D)//from point to right (note that this time does not contain mid is from mid+1, avoid repeating mid) {temp[tempnumber++]=
I
i++; Sort (TEMP,TEMP+TEMPNUMBER,CMP2)//will get the temp according to the ordinate from small to large sort, so once a certain j of the ordinate is greater than I, then the next J also do not have to consider (in fact, personal sense of efficiency gap is not small, after all, you have to sort, But the internet is like this drift ... for (i=0;i<tempnumber;i++) for (j=i+1;j<tempnumber;j++) {if point[temp[j]].y-point[te MP[I]].Y>D) break;//if already greater than D jump out of loop else {d1=distance (point[temp[i]].x,point[te MP[I]].Y,POINT[TEMP[J]].X,POINT[TEMP[J]].Y)//Otherwise see if there are less than D points in the range of d=d<d1?d:d1;
UPDATE}} return D;
int main () {int i,j;
scanf ("%d", &n);
for (i=0;i<n;i++) scanf ("%lf%lf", &point[i].x,&point[i].y);
Sort (point,point+n,cmp); printf ("%.4f\n", Calculate (0,n-1));
return 0;
}