题意:求最近俩点距离的一半
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1007
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+7;
const double eps=1e-8;
const double inf=1e20;
int sgn(double x)
{
if(fabs(x)<eps)return 0;
else return x<0?-1:1;
}
struct Point
{
double x,y;
};
Point p[MAXN],tmp_p[MAXN];
double Distance(Point A,Point B)
{
return sqrt(pow(fabs(A.x-B.x),2)+pow(fabs(A.y-B.y),2));
}
bool cmp(Point A,Point B)
{
if(sgn(A.x-B.x)==0)
{
return sgn(A.y-B.y)<0;
}
else return sgn(A.x-B.x)<0;
}
bool cmpy(Point A,Point B)
{
return sgn(A.y-B.y)<0;
}
double Closest_Pair(int left,int right)
{
double dis=inf;
if(left==right)return dis;
if(left+1==right)
return Distance(p[left],p[right]);
int mid=(left+right)/2;
double d1=Closest_Pair(left,mid);
double d2=Closest_Pair(mid+1,right);
dis=min(d1,d2);
int k=0;
for(int i=left; i<=right; i++)
{
if(fabs(p[mid].x-p[i].x)<=dis)tmp_p[k++]=p[i];
}
sort(tmp_p,tmp_p+k,cmpy);
for(int i=0; i<k; i++)
{
for(int j=i+1; j<k; j++)
{
if(tmp_p[j].y-tmp_p[i].y>=dis)break;
dis=min(dis,Distance(tmp_p[i],tmp_p[j]));
}
}
return dis;
}
int main()
{
int n;
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);
printf("%.2lf\n",Closest_Pair(0,n-1)/2.0);
}
return 0;
}