# include <stdio.h>
# include <algorithm>
# include <math.h>
# define maxn 100009
using namespace std;
struct node
{
double x,y;
}vex[maxn],vex2[maxn];
double min(double a,double b)
{
return a<b?a:b;
}
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int cmpx(node a,node b)
{
return a.x<b.x;
}
int cmpy(node a,node b)
{
return a.y<b.y;
}
double solve(int l,int r)
{
int m,i,j;
if(r==l+1) return dis(vex[r],vex[l]);
if(r==l+2) return min(dis(vex[l],vex[l+1]),min(dis(vex[l],vex[r]),dis(vex[l+1],vex[r])));
double d;
int mid=(r+l)/2;
d=min(solve(l,mid),solve(mid,r));
m=0;
for(i=l;i<=r;i++)
{
if(vex[i].x-vex[mid].x<=d&&vex[i].x-vex[mid].x>=-d)
vex2[m++]=vex[i];
}
sort(vex2,vex2+m,cmpy);
for(i=0;i<m;i++)
for(j=i+1;j<m;j++)
{
if(vex2[j].y-vex2[i].y>=d) break;
d=min(d,dis(vex2[i],vex2[j]));
}
return d;
}
int main ()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&vex[i].x,&vex[i].y);
}
sort(vex,vex+n,cmpx);
double ans=solve(0,n-1);
printf("%.2lf\n",ans/2);
}
return 0;
}
空间最近点对 模板 有待改进 关于分治时的以y的大小排序的改进 算法导论上说可以一次性排序 而不需要重新再排 没看懂,,,,,,
zoj2107
最新推荐文章于 2019-08-06 14:03:10 发布