特判标记即可
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<math.h> 5 #include<algorithm> 6 using namespace std; 7 const double eps = 1e-8; 8 const double inf = 9999999999.0; 9 const int maxn = 100005; 10 struct Point{ 11 double x,y; 12 int flag; 13 }; 14 Point pnt[ maxn<<1 ],temp[ maxn<<1 ]; 15 double dis( Point a,Point b ){ 16 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) ); 17 } 18 int cmpxy( Point a,Point b ){ 19 if( a.x!=b.x ) 20 return a.x<b.x; 21 else 22 return a.y<b.y; 23 } 24 int cmpx( Point a,Point b ){ 25 return a.x<b.x; 26 } 27 int cmpy( Point a,Point b ){ 28 return a.y<b.y; 29 } 30 double solve( int L,int R ){ 31 if( L==R ) 32 return inf; 33 if( L+1==R ){ 34 if( pnt[L].flag==pnt[R].flag ) 35 return inf; 36 else 37 return dis( pnt[L],pnt[R] ); 38 } 39 int mid = (L+R)/2; 40 double res,Ldis,Rdis; 41 Ldis = solve( L,mid ); 42 Rdis = solve( mid+1,R ); 43 res = min( Ldis,Rdis ); 44 int cnt = 0; 45 for( int i=L;i<=R;i++ ){ 46 if( fabs(pnt[i].x-pnt[mid].x)<=res ){ 47 temp[cnt++] = pnt[i]; 48 } 49 } 50 sort( temp,temp+cnt,cmpy ); 51 for( int i=0;i<cnt;i++ ){ 52 for( int j=i+1;j<cnt;j++ ){ 53 if( fabs( pnt[i].y-pnt[j].y )>res ) break; 54 if( pnt[i].flag==pnt[j].flag ) continue; 55 res = min( res,dis(pnt[i],pnt[j]) ); 56 } 57 } 58 return res; 59 } 60 61 int main(){ 62 int ca; 63 scanf("%d",&ca); 64 while( ca-- ){ 65 int n; 66 scanf("%d",&n); 67 for( int i=0;i<n;i++ ){ 68 scanf("%lf%lf",&pnt[i].x,&pnt[i].y); 69 pnt[i].flag = 1; 70 } 71 for( int i=n;i<2*n;i++ ){ 72 scanf("%lf%lf",&pnt[i].x,&pnt[i].y); 73 pnt[i].flag = 2; 74 } 75 sort( pnt,pnt+2*n,cmpxy ); 76 double Ans = solve( 0,2*n-1 ); 77 printf("%.3lf ",Ans); 78 } 79 return 0; 80 }