Ultraviolet A 10245-The closest pair Problem

Source: Internet
Author: User

Question: recent point (big data ).

Analysis: Divide and conquer law. First, sort all vertices by the coordinate. Then, use the sub-governance method to solve the problem.

1. Convert the problem into two subintervals of the same size for separate solutions;

2. The median is center, and the current minimum distance is the Radius Range;

3. Return the minimum value of the last two conditions.

Note: This is the first time we have made such a classic question today.

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;typedef struct nodep{double x,y;}point; point P[10004];bool cmp( point a, point b ){return a.x < b.x;}double dist( point a, point b ){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double mindist( int a, int b ){if ( a > b ) return 40004;int l = (a+b)/2,r = (a+b)/2,mid = (a+b)/2;double d = min( mindist( a, mid-1 ), mindist( mid+1, b ) );while ( l >= a && d > P[mid].x-P[l].x ) l --;while ( r <= b && d > P[r].x-P[mid].x ) r ++;for ( int i = l+1 ; i < r ; ++ i )for ( int j = i+1 ; j < r ; ++ j ) d = min(d,dist(P[i],P[j]));return d;}int main(){int n;while ( ~scanf("%d",&n) && n ) {for ( int i = 0 ; i < n ; ++ i )scanf("%lf%lf",&P[i].x,&P[i].y);sort( P, P+n, cmp );double d = mindist( 0, n-1 );if ( d >= 10000 )printf("INFINITY\n");else printf("%.4lf\n",d);}return 0;}

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.