Topic Portal
The main topic: a given plane n points, find the distance between the two points, so that in all points of the N-point pair, the distance is the smallest of all the points. $n $<=100000.
$Algorithm $
The simplest $n^2$ enumeration is definitely out of the way, and we can only consider $nlogn$ practices in this order of magnitude. Then it is the division that is related to this order of magnitude. The whole plane is divided into two parts, the minimum distance between two parts is calculated, and then the cross-region is processed.
?Steps for solving the divide-and-conquer method:O(Nlogn) by Hzwer
1Set the pointSsl sr 2 Span class= "Fontstyle2" > δ The optimal value obtained in the concentration ( min (δl< Span class= "Fontstyle7" >; Δr) solution.
2δ 2δ number of points, violence? for the past is good.
$Code $
1#include <cstdio>2#include <algorithm>3#include <cmath>4 5 using namespacestd;6 7 intN;8 intque[200090];9 structnode{Ten Doublex, y; One}p[200090]; A BOOLCMP (node A,node b) - { - returna.x<b.x; the } - - BOOLCMP2 (intAintb) - { + returnp[a].y<p[b].y; - } + A DoubleDisintIintj) at { - returnsqrt ((p[i].x-p[j].x) * (p[i].x-p[j].x) + (P[I].Y-P[J].Y) * (p[i].y-p[j].y)); - } - - DoubleMergeintLintR) - { in DoubleDd=1e8; - if(L==R)returnDD; to if(L +1==R)returndis (l,r); + intMid= (l+r) >>1; - DoubleDl=merge (L,mid); the DoubleDr=merge (mid+1, R); *Dd=min (dl,dr); $ Panax Notoginseng intpos=0; - for(inti=l;i<=r;i++) the if(Fabs (p[mid].x-p[i].x) <dd) que[++pos]=i; +Sort (que+1, que+1+pos,cmp2); A for(intI=1; i<=pos;i++) the for(intj=i+1; j<=pos;j++) + { - if(Dis (que[i],que[j]) >dd) Break; $ DoubleDdd=dis (que[i],que[j]); $Dd=min (dd,ddd); - } - returnDD; the } - Wuyi intMain () the { -scanf"%d",&n); Wu for(intI=1; i<=n;i++) scanf ("%LF%LF",&p[i].x,&p[i].y); -Sort (p+1, p+1+n,cmp); Aboutprintf"%.4LF", merge (1, N)); $ return 0; -}
View Code
A few things to note
- The processing of the boundary.
if return DD; if (+1return dis (l,r);
- The use of the square has a constant number of points in the nature, to be timely $break$. Otherwise, it will time out.
- The $l$ line in the figure is probably $mid$. When you start looking for a spot, distance compares with him.
Luogu P1429 plane nearest point to "divide and conquer" by cellur925