http://acm.hdu.edu.cn/showproblem.php?pid=1007
好费劲的一题,但还是被我A了,学到不少东西,骚年继续加油吧!
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <math.h> using namespace std; struct coor { double x,y; }p[100005]; int a[100005]; int cmpx(const coor &a,const coor &b) { return a.x<b.x; } int cmpy(const int &a,const int &b) { return p[a].y<p[b].y; } inline double dist(coor &a,coor &b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } inline double min(double a,double b) { return a<b?a:b; } double merger(int low,int high) { if(low+1==high) return dist(p[low],p[high]); if(low+2==high) return min(dist(p[low],p[high]),min(dist(p[low],p[low+1]),dist(p[low+1],p[high]))); int mid=(low+high)>>1; double ans=min(merger(low,mid),merger(mid+1,high)); int i,j,cnt=0; for(i=low;i<=high;++i) { if(p[i].x>=p[mid].x-ans&&p[i].x<=p[mid].x+ans) a[cnt++]=i; } sort(a,a+cnt,cmpy); for(i=0;i<cnt;++i) { for(j=i+1;j<cnt;++j) { if(p[a[j]].y-p[a[i]].y>=ans) break; ans=min(ans,dist(p[a[i]],p[a[j]])); } } return ans; } int main() { int n,m,i; while(scanf("%d",&n)!=EOF&&n!=0) { for(i=0;i<n;++i) { scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p,p+n,cmpx); printf("%.2lf ",merger(0,n-1)/2); } return 0; }