Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7356 Accepted Submission(s):
2813
Problem Description
There are a lot of trees in an area. A peasant wants to
buy a rope to surround all these trees. So at first he must know the minimal
required length of the rope. However, he does not know how to calculate it. Can
you help him?
The diameter and length of the trees are omitted, which means
a tree can be seen as a point. The thickness of the rope is also omitted which
means a rope can be seen as a line.
There are no more
than 100 trees.
Input
The input contains one or more data sets. At first line
of each input data set is number of trees in this data set, it is followed by
series of coordinates of the trees. Each coordinate is a positive integer pair,
and each integer is less than 32767. Each pair is separated by
blank.
Zero at line for number of trees terminates the input for your
program.
Output
The minimal length of the rope. The precision should be
10^-2.
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
243.06
题意:求凸包周长
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 }
hdu1392(凸包)