hdu 1007 求最近点对
真想知道那些几百ms的是怎么写的
View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct point {
double x,y;
}p[100010],q[100010];
double ans;
inline double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline double min(double a,double b)
{
return a<b?a:b;
}
int cmpx(point a,point b)
{
return a.x<b.x;
}
int cmpy(point a,point b)
{
return a.y<b.y;
}
double divi(int l,int r)
{
if(l==r-1) return dis(p[l],p[r]);
if(l+2==r) return min(dis(p[l],p[r]),min(dis(p[l],p[l+1]),dis(p[l+1],p[r])));
int mid=(l+r)>>1;
ans=min(divi(l,mid),divi(mid+1,r));
int tot=0,i,j;
for(i=l;i<=r;i++)
{
if(p[i].x-p[mid].x<=ans&&p[mid].x-p[i].x<=ans)
q[tot++]=p[i];
}
sort(q,q+tot,cmpy);
for(i=0;i<tot;i++)
{
for(j=i+1;j<tot;j++)
{
if(q[j].y-q[i].y>=ans) break;
ans=min(ans,dis(q[i],q[j]));
}
}
return ans;
}
int main()
{
int n,i;
while(scanf("%d",&n),n)
{
n*=2;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpx);
printf("%.2lf\n",divi(0,n-1)/2);
}
return 0;
}