标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7356 Accepted Submission(s):
2813
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<algorithm> 5 using namespace std; 6 #define N 110 7 struct point 8 { 9 double x,y,angel; 10 } p[N],stack[N]; 11 int top,n; 12 13 double dis(point a,point b) 14 { 15 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 16 } 17 18 bool mult(point p1,point p2,point p0) 19 { 20 return (p1.x-p0.x)*(p2.y-p0.y) >= (p2.x-p0.x)*(p1.y-p0.y); 21 } 22 23 bool cmp(point a,point b) 24 { 25 if(a.angel == b.angel) 26 { 27 if (a.x == b.x) 28 return a.y > b.y; 29 return a.x > b.x; 30 } 31 return a.angel < b.angel; 32 } 33 34 void graham() 35 { 36 int i,k=0; 37 for(i=0; i<n; i++) 38 if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x))) 39 k=i; 40 swap(p[0],p[k]); 41 for(i=1; i<n; i++) 42 p[i].angel=atan2(p[i].y-p[0].y,p[i].x-p[0].x); 43 sort(p+1,p+n,cmp); 44 stack[0]=p[0]; 45 stack[1]=p[1]; 46 stack[2]=p[2]; 47 top=3; 48 for(i=3; i<n; i++) 49 { 50 while(top > 2 && mult(stack[top-2],stack[top-1],p[i])<=0) 51 top--; 52 stack[top++]=p[i]; 53 } 54 } 55 56 int main() 57 { 58 int i; 59 double ans; 60 while(scanf("%d",&n)!=EOF&&n) 61 { 62 for(i=0; i<n; i++) 63 scanf("%lf%lf",&p[i].x,&p[i].y); 64 if(n==1) 65 { 66 printf("0.00\n"); 67 continue; 68 } 69 if(n==2) 70 { 71 printf("%.2lf\n",dis(p[0],p[1])); 72 continue; 73 } 74 graham(); 75 ans=0; 76 for(i=0; i<top-1; i++) 77 ans+=dis(stack[i],stack[i+1]); 78 ans+=dis(stack[top-1],stack[0]); 79 printf("%.2lf\n",ans); 80 } 81 return 0; 82 }
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/lxm940130740/p/3899636.html