#include <cstdio>
#include <cmath>
#include <algorithm>
using std::sort;
using std::min;
#define N 100001
struct Point
{
double x,y;
};
Point pSet[N];
int index[N];
//两点间距离
double dist(Point p,Point q)
{
return sqrt((p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y));
}
bool cmpY(int p, int q)
{
return pSet[p].y < pSet[q].y;
}
bool cmpX(Point p, Point q)
{
return p.x < q.x;
}
double closestPair(int l, int r)
{
//只有两个点或三个点的情况
if(l+1 == r) return dist(pSet[l],pSet[r]);
else if(l+2 == r) return min(dist(pSet[l], pSet[r]), min(dist(pSet[l],pSet[l+1]), dist(pSet[l+1], pSet[r])));
//递归寻找左边一半和右边一半的最近点对的距离,取其中小的
int mid = (l+r) >> 1;
double ans = min(closestPair(l, mid), closestPair(mid+1, r));
int i, j, cnt = 0;
//如果点的x坐标在 pSet[mid].x+ans 和 pSet[mid].x-ans 之间,则记录下来
for( i = l; i <= r; ++i)
if(pSet[i].x <= pSet[mid].x + ans && pSet[i].x >= pSet[mid].x - ans)
index[cnt++] = i;
//将记录下来的点按照y坐标从小到大排序
sort(index, index+cnt, cmpY);
for(i = 0; i < cnt; ++i)
{
for(j = i+1; j < cnt; ++j)
{
//如果纵坐标距离超过ans就不符合条件了
if(pSet[index[j]].y - pSet[index[i]].y >= ans)
break;
ans = min(ans, dist(pSet[index[i]],pSet[index[j]]));
}
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n) && n)
{
for(int i = 0; i < n; ++i)
scanf("%lf %lf",&pSet[i].x,&pSet[i].y);
//按照x坐标从小到大排序
sort(pSet,pSet+n,cmpX);
//因为求的是半径,所以除以2
printf("%.2f\n",closestPair(0,n-1)/2.0);
}
return 0;
}
hdu 1007 Quoit Design(最近点对)
最新推荐文章于 2019-01-23 13:04:25 发布