标签:des c style class blog code
Time Limit: 10000/5000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 29344 Accepted
Submission(s): 7688
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn=100005; 8 int N,f[maxn]; 9 struct Point 10 { 11 double x,y; 12 }p[maxn]; 13 14 double min(double a,double b){ return a<b?a:b;} 15 bool cmpx(const Point &a,const Point &b) 16 { 17 return a.x<b.x; 18 } 19 bool cmpy(const int a,const int b) 20 { 21 return p[a].y<p[b].y; 22 } 23 double dist(int a,int b) 24 { 25 return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y)); 26 } 27 double BinarySearch(int l,int r) 28 { 29 if(l+1==r) 30 { 31 return dist(l,r); 32 } 33 if(l+2==r) 34 { 35 return min(min(dist(l,l+1),dist(l+1,l+2)),dist(l,l+2)); 36 } 37 int mid=(l+r)>>1; 38 double ans=min(BinarySearch(l,mid),BinarySearch(mid+1,r)); 39 int i,j,cnt=0; 40 for(i=l;i<=r;i++) 41 { 42 if(fabs(p[i].x-p[mid].x)<=ans) 43 f[cnt++]=i; 44 } 45 sort(f,f+cnt,cmpy); 46 for(i=0;i<cnt;i++) 47 { 48 for(j=i+1;j<cnt;j++) 49 { 50 if(p[f[j]].y-p[f[i]].y>=ans) break; 51 ans=min(ans,dist(f[i],f[j])); 52 } 53 } 54 return ans; 55 } 56 int main() 57 { 58 while(scanf("%d",&N),N) 59 { 60 for(int i=0;i<N;i++) 61 scanf("%lf %lf",&p[i].x,&p[i].y); 62 sort(p,p+N,cmpx); 63 printf("%.2lf\n",BinarySearch(0,N-1)/2); 64 } 65 return 0; 66 }
hdu 1007 Quoit Design 分治求最近点对,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/xiong-/p/3766726.html