Application of divide and conquer algorithm-minimum distance of nearest point pair-hdu 1007 Quoit Design

Source: Internet
Author: User

Title Description

Gives the n points on the two-dimensional plane, which is half the distance of the nearest two points.

The input contains multiple sets of data, the first action n for each set of data, the number of points, and the next n rows, the coordinates of one point per line. When n is 0, the input ends, and each set of data outputs a row, which is half the distance from the nearest two points.

Input Sample:

2

0 0

1 1

2

1 1

1 1

3

-1.5 0

0 0

0 1.5

0

Sample output:

0.71

0.00

0.75

Topic Analysis:

Using the idea of divide and conquer, the n points are sorted by x-coordinate, and the coordinates mid is divided into two parts, and the distance between the left and right two parts is determined by the nearest point pair respectively, and then the merging is carried out. For the nearest distance d for the two-part, the merging process should check if the band with a width of 2d has two points belonging to two sets and the distance is less than D, the maximum possible n points, the combined time is O (n^2) in the worst case. However, the left and right points have the following sparse properties, For any point on the left, the point on the right must fall in a d*2d rectangle, and a maximum of 6 points (pigeon nest principle) should be checked so that the points of the band band are sorted by y-coordinate and then linearly scanned, so that the time complexity of merging is O (Nlogn).

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5 using namespacestd;6 7 Doublemax=1e10;//defines the maximum distance to return infinity at only one point8 intb; It is used to record subscript, regardless of the problem.9 structnode{Ten     Doublex, y; One     intkey; Key code, optional, related to AB A }; -  -Node ar[100005],br[100005]; the  - BOOLCMPX (Node A,node b) {returna.x<b.x;}//x-coordinate ascending - BOOLCmpy (Node A,node b) {returnA.Y&LT;B.Y;}//y-coordinate ascending - DoubleMinDoubleADoubleb) {returnA&LT;B?A:B;}//returns the minimum value + DoubleDis (Node a,node b) {returnsqrt (Pow (a.x-b.x,2) +pow (A.Y-B.Y,2));}//returns the distance between a point and a point -  + DoubleCalintSinte)//s, E is used to represent the subscript position in the array currently being processed A { at     intMid,i,j,tail=0; Mid represents the position subscript in the middle of the array, tail as a count variable, which is used to store the label of the BR array. -     DoubleD; D represents the distance between point pairs -     if(s==e)returnMAX; If there is only one point -Mid= (s+e)/2; -D=min (Cal (S,mid), Cal (mid+1, E)); Recursion to find the minimum distance between the left and right sides
The following is the point at which the distance from the left point to the right point is less than D, or if there is a point at the right point to the left point that is less than D, and if it exists, it must be in a d*2d rectangle. - for(i=mid;i>=s&&ar[mid].x-ar[i].x<d;i--{//filter the left point, in the middle position to the left of the point within D inbr[tail++]=Ar[i]; - } to + for(i=mid+1; i<e&&ar[i].x-ar[mid].x<d;i++)//Ibid., filter the points on the right - { thebr[tail++]=Ar[i]; * } $Sort (br,br+tail,cmpy); Sorts the points in the rectangle by the y-coordinatePanax Notoginseng for(i=0; i<tail;i++) {//enumerate the distance between the points in the rectangle - for(j=i+1; j<tail&&br[j].y-br[i].y<d;j++){ the if(d>dis (br[i],br[j])) {//Update point value + //a=min (br[i].key,br[j].key); A //b=br[i].key+br[j].key-a; theD=min (D,dis (br[i],br[j)); + } - } $ } $ returnD; Returns the distance between the smallest point pair - } - the intMain () - {Wuyi the intN; - while(cin>>n&&N) { Wu for(intI=0; i<n;i++){ -ar[i].key=i+1; Key code Assignment Aboutscanf"%LF%LF",&ar[i].x,&ar[i].y); $ } -Sort (ar,ar+n,cmpx); Sort by X for AR - DoubleD=cal (0, N); -printf"%.2lf\n", d/2.0); A } + return 0; the}

Application of divide and conquer algorithm-minimum distance of nearest point pair-hdu 1007 Quoit Design

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.