这题卡了一天,上午开始看算法导论,然后实现了,一开始是wa,后来TLE,由于我开始的实现方式比较笨,而且在递归调用的时候很是混乱,用了好多数组。导致我的代码不断的出问题。具体是算法导论33-4.
后来改动了一点,也是看到别人代码改的,我把两个代码都写上,大家一看便知!
在实现分治的时候,我是把左右两个分组都复制了,然后传递的是一个数组,好慢啊。。。改动就是把参数传递改成传的是地址了,这样就不用复制结构体数组啦,不知道节省了多少力气,(⊙o⊙)…
首先是AC代码
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <string.h> 6 #define eps 0.000000001 7 #define S(x) (x)*(x) 8 #define aabs(x) (x)>0?(x):-(x) 9 using namespace std; 10 struct point{ 11 double x,y; 12 }; 13 bool cmp(point a,point b){ 14 if( a.x==b.x) return a.y>b.y; 15 return a.x<b.x; 16 } 17 bool cmpy(point a,point b){ 18 return (a.y>b.y); 19 } 20 struct point P[100000+10]; 21 struct point SCL[100000+10]; 22 double find(int l,int r){ 23 int i,j,k,sn,mid=(l+r)/2; 24 double res=1000000,lres,lx,rx,rres,tmp; 25 if(r-l+1<=3){ 26 for(i=l;i<=r;++i){ 27 for(j=i+1;j<=r;++j){ 28 tmp=sqrt( S(P[i].x-P[j].x) + S(P[i].y-P[j].y) ); 29 if(res-tmp>eps) res=tmp; 30 } 31 } 32 return res; 33 } 34 lres=find(l,mid); rres=find(mid+1,r); 35 res=min(lres,rres); 36 lx=P[mid].x-res; rx=P[mid].x+res; 37 sn=0; 38 for(i=l;i<=r;++i){ 39 if(P[i].x-lx>eps && eps<rx-P[i].x){ 40 SCL[sn].x=P[i].x; SCL[sn].y=P[i].y; sn++; 41 // printf("~~~~~~%lf,%lf ",P[i].x,P[i].y); 42 } 43 } 44 sort(SCL,SCL+sn,cmpy); 45 for(i=0;i<sn;++i){ 46 for(j=i+1;j<sn;++j){ 47 if(SCL[i].y-SCL[j].y>res) break; 48 if(res- sqrt( S(SCL[j].x-SCL[i].x) + S(SCL[j].y-SCL[i].y) ) > eps) 49 res = sqrt( S(SCL[j].x-SCL[i].x) + S(SCL[j].y-SCL[i].y) ); 50 } 51 } 52 return res; 53 } 54 55 56 int main(){ 57 int n; 58 int i,j; 59 while(~scanf("%d",&n)&&n){ 60 for(i=0;i<n;++i){ 61 scanf("%lf%lf",&P[i].x,&P[i].y); 62 } 63 sort(P,P+n,cmp); 64 printf("%.2lf ",find(0,n-1)/2); 65 } 66 return 0; 67 }
然后是TLE代码!
!!!!!!!!!!!!
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <string.h> 6 #define eps 0.000000001 7 #define S(x) (x)*(x) 8 #define aabs(x) (x)>0?(x):-(x) 9 using namespace std; 10 struct point{ 11 double x,y; 12 }; 13 bool cmp(point a,point b){ 14 if( aabs(a.x-b.x) < eps) return (b.y-a.y)>eps; 15 return (b.x-a.x)>eps; 16 } 17 bool cmpy(point a,point b){ 18 return (a.y>b.y); 19 } 20 struct point L[100000+10],R[100000+10]; 21 struct point SCL[100000+10]; 22 double find(int n, struct point P[]){ 23 int i,j,k,ln=0,rn=0,sn,mid=n/2; 24 double res=1000000,lx,rx,lres,rres,tmp; 25 // for(i=0;i<n;++i) printf("%lf %lf ",P[i].x,P[i].y); 26 // printf(" "); 27 if(n<=3){ 28 for(i=0;i<n;++i){ 29 for(j=i+1;j<n;++j){ 30 tmp=sqrt( S(P[i].x-P[j].x) + S(P[i].y-P[j].y) ); 31 if(res-tmp>eps) res=tmp; 32 } 33 } 34 return res; 35 } 36 for(i=0;i<n;++i){ //init the L , R 37 if(i<mid){ 38 L[ln].x=P[i].x; L[ln].y=P[i].y; ln++; 39 }else{ 40 R[rn].x=P[i].x; R[rn].y=P[i].y; rn++; 41 } 42 } 43 lres=find(ln,L); rres=find(rn,R); 44 res=min(lres,rres); 45 lx=P[mid].x-res; rx=P[mid].x+res; 46 sn=0; 47 for(i=0;i<n;++i){ 48 if(P[i].x-lx>eps && eps<rx-P[i].x){ 49 SCL[sn].x=P[i].x; SCL[sn].y=P[i].y; sn++; 50 // printf("~~~~~~%lf,%lf ",P[i].x,P[i].y); 51 } 52 } 53 sort(SCL,SCL+sn,cmpy); 54 for(i=0;i<sn;++i){ 55 for(j=i+1;j<sn;++j){ 56 if(SCL[i].y-SCL[j].y>res) break; 57 if(res- sqrt( S(SCL[j].x-SCL[i].x) + S(SCL[j].y-SCL[i].y) ) > eps) 58 res = sqrt( S(SCL[j].x-SCL[i].x) + S(SCL[j].y-SCL[i].y) ); 59 } 60 } 61 return res; 62 } 63 64 65 int main(){ 66 int n; 67 int i,j; 68 point INIT[100000+10]; 69 while(~scanf("%d",&n)&&n){ 70 for(i=0;i<n;++i){ 71 scanf("%lf%lf",&INIT[i].x,&INIT[i].y); 72 } 73 sort(INIT,INIT+n,cmp); 74 printf("%.2lf ",find(n,INIT)/2); 75 } 76 return 0; 77 }