平面最近点对问题。。
这题以前看过一篇文章。貌似只用比较最近的6个点左右(X轴排序过的)就可以了。。
A B C G
D E F H
假设这8个点在一个矩形上。。可以知道平面最近点对。。就是X轴周围的12个点中。。 O(n)算法就能过掉了。。
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 100010;
const double D_MAX = 1e50;
const double eps = 1e-15;
struct point{
double x,y;
};
point p[MAX];
bool xy(double x,double y) // x < y
{
return x < y - eps;
}
bool dd(double x,double y) // x == y
{
return fabs( x - y ) < eps;
}
bool cmp(point a,point b)
{
if( dd(a.x,b.x) )
return xy(a.y,b.y);
return xy(a.x,b.x);
}
double disp(point a,point b)
{
return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) );
}
double nearpot(int l,int r)
{
double ans = D_MAX;
for(int i = l;i <= r;i++){
for(int j = 1; i+j <= r && j <= 6;j++){
ans = min(ans,disp(p[i],p[j+i]));
}
}
return ans;
}
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 ans = nearpot(0,n-1);
printf("%.2lf\n",ans/2.0);
}
return 0;
}