初始要先按x升序y升序排个序
struct Point
{
double x,y;
Point(){}
Point( double _x , double _y )
{
x = _x; y = _y;
}
Point operator -( const Point&b )const
{
return Point( x-b.x , y-b.y );
}
double operator *( const Point&b )const
{
return x*b.x+y*b.y;
}
};
double dist( Point a , Point b )
{
return sqrt( (a-b)*(a-b) );
}
Point p[maxn],tmp[maxn];
bool cmpxy( Point a , Point b )
{
if ( a.x!=b.x ) return a.x<b.x;
else return a.y<b.y;
}
bool cmpy( Point a , Point b )
{
return a.y<b.y;
}
double Closest_pair( int left , int right )
{
double d = inf;
if ( left+0==right ) return d;
if ( left+1==right ) return dist( p[left] , p[right] );
int mid = ( left+right )/2;
double d1 = Closest_pair( left , mid );
double d2 = Closest_pair( mid+1 , right );
d = min( d1 , d2 );
int k = 0;
for ( int i=left ; i<=right ; i++ )
if ( fabs( p[mid].x-p[i].x )<=d )
tmp[k++] = p[i];
sort ( tmp , tmp+k , cmpy );
for ( int i=0 ; i<k ; i++ )
for ( int j=i+1 ; j<k&&tmp[j].y-tmp[i].y<d ; j++ )
d = min ( d , dist( tmp[i] , tmp[j] ) );
return d;
}