1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<> inline void read(double &x){ scanf("%lf",&x); } template<> inline void read(float &x){ scanf("%f",&x); } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } #define ls(p) Tr[p].lc #define rs(p) Tr[p].rc const int MAXN=2e5+10; const ll INF=1e18; int N,Ct[MAXN]; double ans=INF; struct KDT { int lc,rc; double x,y,l,r,d,u; }Tr[MAXN]; inline double getDist(int i,int j){ return (Tr[i].x-Tr[j].x)*(Tr[i].x-Tr[j].x)+(Tr[i].y-Tr[j].y)*(Tr[i].y-Tr[j].y); } inline bool cmpX(KDT a,KDT b){ return a.x<b.x; } inline bool cmpY(KDT a,KDT b){ return a.y<b.y; } inline void pushUp(int p) { Tr[p].l=Tr[p].r=Tr[p].x; Tr[p].u=Tr[p].d=Tr[p].y; if(ls(p)) checkMin(Tr[p].l,Tr[ls(p)].l),checkMax(Tr[p].r,Tr[ls(p)].r),checkMin(Tr[p].d,Tr[ls(p)].d),checkMax(Tr[p].u,Tr[ls(p)].u); if(rs(p)) checkMin(Tr[p].l,Tr[rs(p)].l),checkMax(Tr[p].r,Tr[rs(p)].r),checkMin(Tr[p].d,Tr[rs(p)].d),checkMax(Tr[p].u,Tr[rs(p)].u); } int build(int l,int r) { if(l>r) return 0; if(l==r) return pushUp(l),l; int mid=(l+r)>>1; double avx=0,avy=0,svx=0,svy=0; for(int i=l;i<=r;++i) avx+=Tr[i].l,avy+=Tr[i].r; avx/=1.0*(r-l+1),avy/=1.0*(r-l+1); for(int i=l;i<=r;++i) svx+=(Tr[i].x-avx)*(Tr[i].x-avx),svy+=(Tr[i].y-avy)*(Tr[i].y-avy); if(svx>=svy) Ct[mid]=1,std::nth_element(Tr+l,Tr+mid,Tr+r+1,cmpX); else Ct[mid]=2,std::nth_element(Tr+l,Tr+mid,Tr+r+1,cmpY); ls(mid)=build(l,mid-1),rs(mid)=build(mid+1,r); return pushUp(mid),mid; } inline double otDist(int s,int id) { double ret=0; if(Tr[id].l>Tr[s].x) ret+=(Tr[id].l-Tr[s].x)*(Tr[id].l-Tr[s].x); if(Tr[id].r<Tr[s].x) ret+=(Tr[id].r-Tr[s].x)*(Tr[id].r-Tr[s].x); if(Tr[id].d>Tr[s].y) ret+=(Tr[id].d-Tr[s].y)*(Tr[id].d-Tr[s].y); if(Tr[id].u<Tr[s].y) ret+=(Tr[id].u-Tr[s].y)*(Tr[id].u-Tr[s].y); return ret; } void query(int l,int r,int x) { if(l>r) return ; int mid=(l+r)>>1; if(mid!=x) checkMin(ans,getDist(x,mid)); if(l==r) return ; double dl=otDist(x,ls(mid)),dr=otDist(x,rs(mid)); if(dl<ans&&dr<ans) { if(dl<dr) { query(l,mid-1,x); if(dr<ans) query(mid+1,r,x); } else { query(mid+1,r,x); if(dl<ans) query(l,mid-1,x); } } else { if(dl<ans) query(l,mid-1,x); if(dr<ans) query(mid+1,r,x); } } int main() {
read(N); for(int i=1;i<=N;++i) read(Tr[i].x,Tr[i].y); build(1,N); for(int i=1;i<=N;++i) query(1,N,i); printf("%.4lf",std::sqrt(ans)); return 0; }
|