Raid POJ - 3714 分治最近点对模板
复杂度大约是nloglog
1 //#include<bits/stdc++.h> 2 //#pragma comment(linker, "/STACK:1024000000,1024000000") 3 #include<stdio.h> 4 #include<algorithm> 5 #include<queue> 6 #include<string.h> 7 #include<iostream> 8 #include<math.h> 9 #include<set> 10 #include<map> 11 #include<vector> 12 #include<iomanip> 13 #include<bitset> 14 using namespace std; // 15 16 #define ll long long 17 #define pb push_back 18 #define FOR(a) for(int i=1;i<=a;i++) 19 #define sqr(a) (a)*(a) 20 #define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)) 21 ll qp(ll a,ll b,ll mod){ 22 ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t; 23 } 24 struct DOT{ll x;ll y;}; 25 inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;} 26 const int dx[4]={0,0,-1,1}; 27 const int dy[4]={1,-1,0,0}; 28 const int inf=0x3f3f3f3f; 29 const ll mod=1e9+7; 30 31 const int maxn=2e5+10; 32 int n; 33 struct NODE{ 34 double x,y; 35 bool bel; 36 }a[maxn],tmp[maxn]; 37 38 bool cmpx(NODE a,NODE b){return a.x<b.x;} 39 bool cmpy(NODE a,NODE b){return a.y<b.y;} 40 41 double make(int l,int r){ 42 if(l==r)return 9e18; 43 int m=l+r>>1; 44 int cnt=0; 45 double ans=min(make(l,m),make(m+1,r)); 46 for(int i=l;i<=r;i++){ 47 if(fabs(a[i].x-a[m].x)<=ans) 48 tmp[++cnt]=a[i]; 49 } 50 sort(tmp+1,tmp+1+cnt,cmpy); 51 for(int i=1;i<=cnt;i++){ 52 for(int j=i+1;j<=cnt;j++){ 53 if(tmp[j].y-tmp[i].y>ans)break; 54 if(tmp[i].bel==tmp[j].bel)continue; 55 ans=min(ans,dis(tmp[i],tmp[j])); 56 } 57 } 58 return ans; 59 } 60 61 int main(){ 62 int T;scanf("%d",&T); 63 while(T--){ 64 scanf("%d",&n); 65 for(int i=1;i<=n;i++){ 66 scanf("%lf%lf",&a[i].x,&a[i].y); 67 a[i].bel=0; 68 } 69 for(int i=1+n;i<=n+n;i++){ 70 scanf("%lf%lf",&a[i].x,&a[i].y); 71 a[i].bel=1; 72 } 73 sort(a+1,a+2*n+1,cmpx); 74 printf("%.3f\n",make(1,2*n)); 75 } 76 }