Plane nearest point pair (divide and conquer Nlogn)

Source: Internet
Author: User

Planar nearest point pair, refers to the n points on the plane to find the minimum distance between the points

First you can sort by X for the first keyword, and then each time according to the X division, the left to find a shortest distance D1, the right also find a shortest distance D2, then take D=min (D1, D2)

Then just consider the point across the left and right side, and enumerate the points on the right pi

Then it is obvious that if the pi distance from the middle of the point exceeds D, it can be directly shed, just consider the distance between the midpoint of the point less than D

This makes it possible to draw a square with a side length of 2d for each pi, which is easy to pass, with a maximum of 8 points in the rectangle.

So the key question is to find these 8 points quickly.

The simple approach is to nlognlogn the points after the division, so the complexity is

But if we combine the sorting, each division of the process in passing by the Y-merge sort, you can save the Logn (%%% think of the practice and Xin God Ben)

The code is as follows

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;structp{intx, y; BOOL operator< (Constp& B)Const{returnX <b.x;}} p[100050];intDis (P &a, p &b) {return(a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y); }p q[100050];intDivide (intLintR) {    if(L = = r)return1e7; intMid = (l+r) >>1, d, tx = p[mid].x, tot =0; D= Min (Divide (L, mid), Divide (mid+1, R));  for(inti = l, j = mid+1; (I <= Mid | | J <= r); i++)    {         while(J <= R && (p[i].y > P[j].y | | i > MID)) q[tot++] = P[j], j + +;//merge by y sort        if(ABS (P[I].X-TX) < D && I <= mid)//Select a point in the middle that meets the requirements        {             for(intK = J1; K > Mid && J-k <3; k--) d =min (d, Dis (p[i], p[k]));  for(intK = J; K <= R && k-j <2; k++) d =min (d, Dis (p[i], p[k])); }        if(I <= mid) q[tot++] =P[i]; }     for(inti = l, j =0; I <= R; i++, J + +) P[i] =Q[j]; returnD;}intMain () {intN; CIN>>N;  for(inti =1; I <= N; i++) cin>>p[i].x>>p[i].y; Sort (P+1, p+1+N); cout<<divide (1, N) <<Endl;}

Note: Only points with integer coordinates are selected here, and the range is small and requires some change to use

Plane nearest point pair (divide and conquer Nlogn)

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.