P1429 flat closest point (enhanced version), p1429 enhanced version

Source: Internet
Author: User

P1429 flat closest point (enhanced version), p1429 enhanced version
Description

Given n points on the plane, find the distance between a pair of points, so that the distance is the smallest among all the points of the n points.

Input/Output Format

Input Format:

 

Row 1: n; 2 ≤ n ≤ 200000

Next n rows: each row has two real numbers: x y, which indicate the row coordinates and column coordinates of a point, separated by a space in the middle.

 

Output Format:

 

Only one row, a real number, indicates the shortest distance, accurate to four digits after the decimal point.

 

Input and Output sample input sample #1:
31 11 22 2
Output sample #1:
1.0000
Description

0 <= x, y <= 10 ^ 9

 

 

I really cannot understand the relationship between this question and computational ry ,,

But it is also a good question,

Sort each vertex (struct) in ascending order of x

First, when we use the binary idea to divide points into the last two points

Then update the answer using the distance between the two points (the answer is initialized to an infinite number)

Then we need to find the specific implementation scheme of Binary-find the middle of these vertex numbers every time we divide them into some points (because the points are sorted in order, that is, find the dividing line on the X axis ), the entire set of points is roughly divided into two parts.

On the right side of the demarcation line, add the point with a distance less than ans to the auxiliary struct.

On the left side of the demarcation line, constant enumeration points are listed. When the off-boundary line is less than ans, the points in the auxiliary struct are located one by one (which will be explained later) to determine whether the distance is less than ans.

 

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=200001; 7 inline void read(int &n) 8 { 9     char c=getchar();bool flag=0;n=0;10     while(c<'0'||c>'9')    c=='-'?flag=1,c=getchar():c=getchar();11     while(c>='0'&&c<='9')    n=n*10+(c-48),c=getchar();if(flag==1)n=-n;12 }13 struct Point14 {15     double x,y;16 }point[MAXN],a[MAXN];17 int comp(const Point &a,const Point &b)18 {return (a.x==b.x)?a.y<b.y:a.x<b.x;}19 double ans=43843843800.0;20 void work(int l,int r)21 {22     if(l==r)    return ;23     int mid=(l+r)>>1;24     work(l,mid);work(mid+1,r);25     int t=0;26     for(int i=mid+1;i<=r;i++)27         if(point[i].x-point[mid].x<ans)28             a[++t]=point[i];29     for(int i=l;i<=mid;i++)30         if(point[mid].x-point[i].x<=ans)31             for(int k=1;k<=t;k++)32                 ans=min(ans,(double) sqrt( (point[i].x-a[k].x)*(point[i].x-a[k].x)+(point[i].y-a[k].y)*(point[i].y-a[k].y) ));33 }34 int main()35 {36     int n;37     read(n);38     for(int i=1;i<=n;i++)        scanf("%lf%lf",&point[i].x,&point[i].y);39     sort(point+1,point+n+1,comp);40     work(1,n);41     printf("%.4lf",ans);42     return 0;43 }

 

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.