题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2107
http://acm.hdu.edu.cn/showproblem.php?pid=1007
Quoit Design
题目大意:寻找最近点对。
直接套模板即可!!!
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
struct point
{
double x,y;
}p[100010];
int n;
bool cmp_x(point a,point b)
{
return a.x<b.x;
}
bool cmp_y(point a,point b)
{
return a.y<b.y;
}
double closest_pair(point *p,int n)
{
if(n<=2)
{
return sqrt((p[1].x-p[0].x)*(p[1].x-p[0].x)+(p[1].y-p[0].y)*(p[1].y-p[0].y));
}
int m=n/2;
double x=p[m].x;
double d=min(closest_pair(p,m),closest_pair(p+m,n-m));
inplace_merge(p,p+m,p+n,cmp_y);
vector<point> b;
for(int i=0;i<n;i++)
{
if(fabs(p[i].x-x)>=d)
continue;
for(int j=0;j<b.size();j++)
{
double dx=p[i].x-b[b.size()-j-1].x;
double dy=p[i].y-b[b.size()-j-1].y;
if(dy>=d)
break;
d=min(d,sqrt(dx*dx+dy*dy));
}
b.push_back(p[i]);
}
return d;
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmp_x);
printf("%.2lf\n",closest_pair(p,n)/2);
}
return 0;
}