Rokua the nearest point on the plane of P1257 to the sorting of mathematical divide and cure

Source: Internet
Author: User

From the Rocky Valley.

Method One:

The distance between the 1th point and the remaining n-1 points is first obtained;

Then the distance between the 2nd point and the remaining n-2 points is obtained;

Then the distance between the 3rd point and the remaining n-3 points is obtained;

................................................

Then the distance between the first n-1 point and the remaining 1 points is obtained;

Then find the minimum value.

So the complexity of the algorithm is O (n^2), obviously can not meet the needs of the subject. But...... It seems that the Valley God machine can ... Have a ...

Welcome to the simple classmate Challenge data enhanced version of the new

Method Two:

Consider the following divide-and-conquer algorithm:

The points in the set plane are all in the point set S, in order to divide s linearly into 2 subsets S1 and S2 of roughly equal size, we select a vertical line L (equation: x=m) as the split line. where M is the median of the x-coordinate of each point in S. This divides S into s1={p∈s|px≤m} and S2={p∈s|px>m}. Thus the S1 and S2 are located on the left and right side of the line L, respectively, and S=S1∪S2. Because M is the median of the x-coordinate values of the points in S, the points in S1 and S2 are roughly equal. We get the minimum distance δ1 and δ2 in S1 and S2, respectively, on the S1 and S2 to the nearest point to the problem. Δ=min (Δ1,Δ2) is now established.

If the distance between the nearest point pair (p,q) of S is <δ D (p,q), then p and Q must belong to S1 and S2. You may wish to set P∈S1,Q∈S2. Then the distance between P and Q is less than δ of linear L. Therefore, if we use P1 and P2 respectively to represent the left and right of the line L 2 vertical bars with a width of δ, then P∈S1,Q∈S2

At this point, a pair of points in P1 with all points in the P2 is the closest candidate to the point pair. In the worst case there are N^2/4 to such a candidate. But the points in P1 and P2 have the following sparse properties, which allows us to not have to examine all this N2/4 to the candidate. Consider any point P in P1, which, if it is the candidate with the point Q in the P2 closest to the point pair, there must be D (p,q) <δ. How many points are there in the P2 that satisfy this condition? It is easy to see that such a point must fall in a δx2δ rectangle R,

The meaning of Δ indicates that the distance of any 2 s in P2 is not less than δ. This allows you to eject a point in the rectangle R of up to 6 S. In fact, we can divide the rectangle R's length of 2δ by 3, dividing it by 2 equal to the side of Δ, thereby exporting 6 (Δ/2) x (2Δ/3) rectangles.

So d (u,v) ≤5δ/6<δ. This contradicts the meaning of δ. That is, the rectangle R has a maximum of 6 points in S. Because of this sparse nature, there are up to 6 points in any p,p2 in P1 that are candidates for the closest point pair. Therefore, in the consolidation step of the division method, we only need to examine 6xn/2=3n to the candidate, not N^2/4 to the candidate.

We only know that the point P in each S1 in P1 needs to check at most 6 points in the P2, but we don't know exactly which 6 points to check. To solve this problem, we can project all the S2 points in P and P2 onto the vertical line L. Since the midpoint of the S2 that can form the closest point to the P-point pair of candidates is bound to be in the rectangular r, the distance from the projection point on the line L to the projection point of P on L is less than Δ. The above analysis shows that this projection point has a maximum of 6. Therefore, if the P1 and P2 all the points in the y-coordinate of the order, then the P1 in a bit p, the order of the point column for a scan, you can find all the closest to the point pair of candidates, the P1 in every point as long as the check P2 in the order of the successive 6 points.

At this point, we use the division of the method to find out the closest points to the plane.

In fact, I think this method is nlogn^2.

But can be optimized into NLOGN, but in fact, more than a log is not much difference 233

1Type rec=Record2 X,y:int64;3 end;4 varA:array [0..200001] of rec;5Tt,ttt,tttt:array [0..200001] of rec;6 N,l,m,min:int64;7 I,j,k:longint;8 procedure sort (l,r:longint);9 varI,j:longint;Ten X,y:rec; One begin Ai:=l; -j:=R; -x:=a[(l+r) div2]; the Repeat -      while(a[i].x<x.x) or ((a[i].x=x.x) and (A[I].Y&GT;X.Y)) DoInc (i); -      while(x.x<a[j].x) or ((x.x=a[j].x) and (X.Y&GT;A[J].Y)) DoDec (j); -     ifNot (i>j) +  Then - begin +y:=A[i]; Aa[i]:=A[j]; ata[j]:=y; - Inc (i); -j:=j-1; - end; -Until i>J; -   ifl<J then sort (l,j); in   ifi<R then sort (i,r); - end; to procedure Sortt (l,r:longint); + varI,j:longint; - X,y:rec; the begin *i:=l; $j:=R;Panax Notoginsengx:=tttt[(l+r) div2]; - Repeat the      while(TTTT[I].Y&LT;X.Y) DoInc (i); +      while(X.Y&LT;TTTT[J].Y) DoDec (j); A     ifNot (i>j) the  Then + begin -y:=Tttt[i]; $tttt[i]:=Tttt[j]; $tttt[j]:=y; - Inc (i); -j:=j-1; the end; -Until i>J;Wuyi   ifl<J then Sortt (l,j); the   ifi<R then Sortt (i,r); - end; Wu function Dis (A,B:REC): Int64; - begin AboutExit (SQR (a.x-b.x) +SQR (a.y-b.y)); $ end; - function Minn (a,b:int64): Int64; -BeginifA<b then exit (a)Elseexit (b); end; - function Work (le,ri:longint): Int64; A varTl,tr,t1,t2,min:int64; + I,j,k,lenr,lenl,p1,p2:longint; the begin -   ifri-le=1Then exit (DIS (a[ri],a[le)); $   ifri-le=2Then EXIT (MINN (Minn (DIS (a[ri],a[le)), Dis (a[ri-1],a[le]), DIS (a[le+1],a[ri] )); theTl:=work (Le, (Le+ri) div2); theTr:=work ((Le+ri) div2)+1, RI); themin:=Minn (TL,TR); thet1:= (Le+ri) div2; -t2:=t1+1; in    while(T1>=le) and (Sqr (a[t1].x-a[(Le+ri) div2].x) <=min) DoDec (t1); the Inc (T1); the    while(T2<=ri) and (Sqr (a[t2].x-a[(Le+ri) div2].x) <=min) DoInc (T2); About Dec (t2); the   if(t2= (Le+ri) div2) or (t1= (Le+ri) div2)+1) then exit (min); the    forK:=t1 to T2 Dotttt[k]:=A[k]; the Sortt (T1,T2); +    forK:=t1 to t2-1  Do - begin the        forj:=k+1To Minn (k +8, T2) DoBayi begin themin:=Minn (Min,dis (TTTT[K],TTTT[J)); the end; - end; - exit (min); the end; the begin the READLN (n); the    fori:=1to n Doreadln (A[I].X,A[I].Y); -Sort1, n); theWriteln (sqrt (Work (1, N)):0:4); theEnd.

Rokua the nearest point on the plane of P1257 to the sorting of mathematical divide and cure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.