#include<bits/stdc++.h>
using namespace std;
const double INF = 0x3f3f3f3f;
struct Point
{
double x,y;
}point[100005];
int p[100005];
bool cmpx(struct Point a,struct Point b){ return a.x < b.x; }
bool cmpy(int a,int b){ return point[a].y < point[b].y; }
double get_distance(double x1,double y1,double x2,double y2){ return sqrt( pow(x1-x2,2) + pow(y1-y2,2) ); }
double ClosetPoints(int left,int right)//S为平面上N个点的集合
{
if(left == right) return INF;//只有一个点
if(left + 1 == right) return get_distance(point[left].x,point[left].y,point[right].x,point[right].y);//只有两个点
int mid = (left+right)>>1;
double left_min = ClosetPoints(left,mid),right_min = ClosetPoints(mid + 1,right);//递归求解
double d = min(left_min,right_min);
int k = 0;
for(int i = left; i <= right; i++)//分离出x-d到x+d的区间
{
if(fabs(point[i].x-point[mid].x)<=d)
{
p[k++]=i;
}
}
sort(p,p+k,cmpy);
for(int i=0;i<k;i++)
{
for(int j = i + 1; j < k && point[p[j]].y - point[p[i]].y < d; j++)
{
d=min(d,get_distance(point[p[i]].x,point[p[i]].y,point[p[j]].x,point[p[j]].y));
}
}
return d;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i = 0; i < n; i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);
}
sort(point,point+n,cmpx);
printf("%.2lf\n",ClosetPoints(0,n-1)/2.0);
}
return 0;
}
/*
int ClosestPoints(S)
{
1. if(n<2) return ∞;
2. m=s中各点x坐标的中位数(平均分为两个集合)
3. 构造S1和S2,使得S1中点的x坐标小于m,S2中点的x坐标大于m;
4. d1=ClosestPoints(S1),d2=ClosestPoints(S);
5. d=min(d1,d2);
6. 构造P1,P2,使得P1是S1中点的x坐标与m的距离小于d的点集,P2是S2中点的x坐标与m的距离小于d的点集
7. 将P1,P2中的点按y坐标升序排序// 进行预排序会减少时间
8. 对P1中的每一个点p,在P2中查找与点p的y坐标小于d的点(最多6个点),并求出其中最小距离d3
9. return min(d,d3);
}
*/
HDU ~ 1007 ~ Quoit Design(分治法)(最近点对)
最新推荐文章于 2022-01-26 18:38:33 发布