hdu1007-Quoit Design

http://acm.hdu.edu.cn/showproblem.php?pid=1007

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std ;
const int  maxn = 1000001 ;
const int  INF = 1000000001 ;
struct Point
{
	double x , y ;
}point[ maxn ] ;
int n ;
int temp[ maxn ];

bool cmp(const  Point& a , const Point& b )
{
	if( a.x == b.x )
		return  a.y < b.y ;
	else
		return a.x < b.x ;
}

bool cmpy( const int& a , const int& b )
{
	return point[ a ].y < point[ b ].y ;
}

double min( double a , double b )
{
	return a < b ? a : b ;
}

double dist( int i , int j )
{
	return sqrt( (point[ i ].x - point[ j ].x) * ( point[ i ].x - point[ j ].x ) + ( point[ i ].y - point [ j ].y ) * ( point[ i ].y - point[ j ].y ) ) ;
}

double merge( int left , int right )
{
	double d = INF ;
	if( left == right )
		return d ;
	if( left + 1 == right )
		return dist( left , right ) ;

	int mid = ( left + right ) >> 1 ;
	double d1 = merge( left , mid ) ;
	double d2 = merge( mid + 1 , right ) ;
	d = min( d1 , d2 ) ;
	int i , j , k = 0 ;
	for( i = left ; i <= right ; ++i )
	{
		if( fabs( point[ mid ].x - point[ i ].x ) <= d )
			temp[ k++ ] = i ;
	}
	sort( temp , temp + k , cmpy ) ;
	for( i = 0 ; i < k ; ++i )
		for( j = i + 1 ; j < k &&  point[ temp[ j ] ].y - point[ temp[ i ] ].y  < d ; ++j )
		{
			double d3 = dist( temp[ i ] , temp[ j ] ) ; 
			if( d > d3 )
				d = d3 ;
		}
	return d ;
}

int main()
{
	while( scanf( "%d" , &n ) && n )
	{
		for(int  i = 0 ; i < n ; ++i )
		{
			scanf( "%lf%lf" , &point[ i ].x , &point[ i ].y ) ;
		}
		sort( point , point + n , cmp ) ;
		printf( "%.2lf\n" , merge( 0 , n - 1 ) / 2 ) ;
	}
	return 0 ;
}


你可能感兴趣的:(分治法,杭电,最近点对)