HDU - 1007 Quoit Design
平面最近点对模板。
//Achen #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<queue> #include<cmath> const int N=1e5+7; typedef long long LL; typedef double db; using namespace std; int T,n; template<typename T>void read(T &x) { char ch=getchar(); x=0; T f=1; while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') f=-1,ch=getchar(); for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f; } struct pt { db x,y; }p[N],tp[N]; bool cmp(const pt&A,const pt&B) { return A.x<B.x||(A.x==B.x&&A.y<B.y); } bool cmpy(const pt&A,const pt&B) { return A.y<B.y; } db sqr(db x) {return x*x;} db dis(pt A,pt B) { return sqrt(sqr(A.x-B.x)+sqr(A.y-B.y)); } db solve(int l,int r) { db d=1e18; if(l>=r) return d; if(l+1==r) return dis(p[l],p[r]); int mid=((l+r)>>1); d=min(d,solve(l,mid)); d=min(d,solve(mid+1,r)); int k=0; for(int i=l;i<=r;i++) if(fabs(p[i].x-p[mid].x)<=d) tp[++k]=p[i]; sort(tp+1,tp+k+1,cmpy); for(int i=1;i<=k;i++) for(int j=i+1;j<=k&&tp[j].y-tp[i].y<d;j++) d=min(d,dis(tp[i],tp[j])); return d; } int main() { #ifdef DEBUG freopen(".in","r",stdin); freopen(".out","w",stdout); #endif for(;;) { read(n); if(!n) break; for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); sort(p+1,p+n+1,cmp); printf("%.2lf\n",solve(1,n)/2); } return 0; }