Morning head a bit of pain, suddenly thought can use KD Tree solution plane nearest point to the problem, found a way to test, the result can, although inefficient, but still AC ~
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1007
The topic requires half of the distance between the closest points on the plane.
The idea is to set up a tree first, a little bit into the tree, and then query its nearest point for each point, the enumeration finds the minimum value. Pay attention to the query when you don't let yourself compare yourself with yourself. Personal feeling, this kind of writing can also achieve O (nlogn) complexity. Build partition between, according to X, y span big one to divide, should be close to O (Nlogn), but I am too lazy, that method after try again, now first X, y rotate come.
The KD tree code is as follows:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5#include <cstdio>6#include <cmath>7 #defineMAX (101000)8 #definePOW (x) ((x) * (x))9 Ten using namespacestd; One A intN, idx; - - structpoint{ the Doublex[2]; - BOOL operator< (ConstPoint &u)Const{ - returnX[IDX] <U.x[idx]; - } + }po[max]; - + structTree { APoint p[max<<2]; at intson[max<<2]; - BOOLf[max<<2]; - intps[max<<2]; - voidBuild (intLintRintU =1,intDEP =0) - { - if(L > R)return; inSon[u] = Rl; -son[u<<1] = son[u<<1|1] = -1; toIDX = dep%2; + intMid = (l+r) >>1; -Nth_element (Po+l, Po+mid, po+r+1); theP[u] = Po[mid], ps[u] =mid; *Build (L, mid-1, u<<1, dep+1 ); $Build (mid+1, R, u<<1|1, dep+1 );Panax Notoginseng } - the DoubleQuery (Point A,intIdintU =1,intDEP =0){ + if(Son[u] = =-1)return 1000000000.0; A the Doubledis = POW (p[u].x[0]-a.x[0]) + POW (p[u].x[1]-a.x[1]); + if(Ps[u] = = id) dis =1000000000.0; - intDim = dep%2, FG =0; $ intx = u<<1, y = u<<1|1; $ if(A.x[dim] >=P[u].x[dim]) swap (x, y); - DoubleTM1 =1000000000.0, TM2 =1000000000.0; - if(~son[x]) TM1 = Query (A, ID, X, dep+1); the if(Pow (A.x[dim]-P[u].x[dim]) < TM1) FG =1; - if(~son[y] && FG) TM2 = Query (A, id, y, dep+1);Wuyi if(Dis > tm1) dis =TM1; the if(Dis > tm2) dis =tm2; - returndis; Wu } - }KD; About $ - intMain () { - while(~SCANF ("%d", &N), N) { - for(inti =0; I < n; i++) Ascanf"%LF%LF", &po[i].x[0], &po[i].x[1]); +memset (KD.F,0,sizeof(KD.F)); thememset (Kd.ps,-1,sizeof(kd.ps)); -Kd.build (0, N-1); $ DoubleAns =1000000000.0; the for(inti =0; I < n; i++){ the DoubleTM =Kd.query (Po[i], i); the if(tm < ans) ans =TM; the } -printf"%.2lf\n", sqrt (ans)/2); in } the return 0; the}
The nearest point pair of KD tree solution plane