hdu 1007 Quoit Design(最近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=1007
一看题目就是一个很裸的最近点对。。暴力的做法是o(n^2)而对于o(nlogn)算法知识理论上理解了,但是还没实现过,好不容西写出来还是tle最后在gsb的指点下总算是A了。。汗。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
const int max_s = 100007;
using namespace std;
const int M = 99999999.0;
struct node
{
double x,y;
}p[max_s];
int a[max_s];
int cmp_x(node a,node b)
{
return a.x>b.x;
}
int cmp_y(int s,int e)
{
return p[s].y>p[e].y;
}
double dis(node t1,node t2)
{
double x=t1.x-t2.x;
double y=t1.y-t2.y;
return sqrt(x*x+y*y);
}
double short_s(int l,int r)//分治的思想
{
int i,j;
if(l==r) return M;
else if(l+1==r) return dis(p[l],p[r]);
int m=(l+r)>>1;
double Min=min(short_s(l,m),short_s(m+1,r));
int k=0;
for(i=l;i<=r;i++)//这里是从L开始的。。
{
if(p[i].x>=p[m].x-Min&&p[i].x<=p[m].x+Min)
a[k++]=i;
}
sort(a,a+k,cmp_y);
for(i=0;i<k-1;i++)
{
for(j=i+1;j<k;j++)
{
if(p[a[i]].y-p[a[j]].y>=Min)
break;
Min=min(Min,dis(p[a[j]],p[a[i]]));
}
}
return Min;
}
int main()
{
//freopen("d.txt","r",stdin);
int n,i;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmp_x);//sort的速度要比qsort的快一些貌似。。
printf("%.2lf\n",short_s(0,n-1)/2);
}
return 0;
}


浙公网安备 33010602011771号