Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1007
Test instructions: give you the coordinates of N (2<=n<=10^6) points, then find two points so that the distance between them is the smallest, then the output is half the minimum distance;
First the n points in the x-coordinate order, and then the left N/2 and the right n/2 the nearest distance, and finally merged.
First, the false SetPoint is n, numbered 1 to N. We want to divide and conquer, then find a middle number mid, first of all 1 to mid point of the nearest distance is set to D1, and mid+1 to n the nearest distance is set to D2. The points here need to be sorted in the order of the x-coordinates, and assume that there is no 2 o'clock in the same position in these points. (if any, the direct minimum distance is 0).
Then, make d the D1, the smaller point in the D2. If two points in the nearest point pair are in the 1-mid collection, or mid+1 to the N collection, then D is the minimum distance. But it is also possible that the nearest point pair of two points belong to these two sets, so we must first check if this situation will exist, if any, then the nearest point to the distance recorded, to update d. So we can get to the minimum distance d.
The key is to detect the nearest point pair, in theory each point should be matched with the point of the opposite set, that efficiency is not enough to meet our requirements. So here to optimize. How to optimize it? Consider, if we choose the Division point Mid, if the horizontal axis of a certain point of the horizontal axis of the absolute value of more than D1 and more than D2, then this point to the mid-point distance is bound to exceed D1 and D2 of the small, so this point to the other side of the collection of any point of the distance must not be the smallest.
So let's start by filtering out all the points in the range of mid to the right, and putting them in a set. After screening, of course, you can take these points 22 distance to update D, but this is still very slow, in case the point of satisfying conditions a lot of it. We have to continue to optimize here. Start by sorting the points in y-coordinates. Suppose that there are CNT points after sorting, numbered 0 to Cnt-1. Then we use number No. 0 and 1 to the cnt-1 point to find the distance, then 1th and 2 to cnt-1 points to find the distance ... If the y-axis distance of the two points already exceeds D, the loop can break directly and start looking for the next point.
#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<bitset>#include<iostream>#include<time.h>#include<vector>#include<queue>typedefLong LongLL;using namespacestd;Const intN = 1e6+1;Const DoubleEPS = 1e-Ten;Const intINF =0x3f3f3f3f;Const intMoD =1000000007;Const DoublePI =4*atan (1.0);structpoint{Doublex, y;} P[n], t[n];DoubleDist (Point P1, point p2) {returnsqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (p1.y-p2.y));}BOOLcmpx (Point p1, point p2) {returnp1.x <p2.x;}BOOLCmpy (Point p1, point p2) {returnP1.y <p2.y;}DoubleFind (intLintR) { if(L +1= = R)///when there are only two points; returnDist (p[l], p[r]); if(L +2= = R)///three points of time; returnMin (min (Dist (p[l), p[l+1]), Dist (p[l], p[r)), Dist (p[l+1], p[r]); intMid = (l+r)/2; DoubleMin_d = Min (Find (L, Mid), find (mid+1, R));///find the minimum value on both sides and update the middle part below; intCNT =0; for(intI=l; i<=r; i++) { if(Fabs (p[i].x-p[mid].x) <=min_d) t[cnt+ +] = P[i];///Add the points in the T set that may be the nearest point pair;} sort (T, T+CNT, Cmpy);///sort, found below, T set the distance between the nearest two points, with the new Min_d; for(intI=0; i<cnt; i++) { for(intj=i+1; j<cnt; J + +) { if(T[j].y-t[i].y >Min_d) Break; Min_d=min (Min_d, Dist (T[i], t[j])); } } returnMin_d;}intMain () {intN; while(SCANF ("%d", &N), N) { for(intI=0; i<n; i++) scanf ("%LF%LF", &p[i].x, &p[i].y); Sort (p, p+n, CMPX);///Sort by horizontal axis x; DoubleAns = Find (0, N-1);///recursive solution;printf ("%.2f\n", ans/2); } return 0;}View Code
Quoit Design---hdu1007 (nearest point to Problem division method)