Surround the Trees
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7164 Accepted Submission (s): 2738
Problem Descriptionthere is 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 is 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 is no more than trees.
Inputthe input contains one or more data sets. At first line of each input data set are number of trees in this data set, it is followed by series of coordinates of the T Rees. Each coordinate is a positive an integer pair, and each of the integers is less than 32767. Each pair are separated by blank.
Zero at line for number of trees terminates the input for your program.
Outputthe minimal length of the rope. The precision should be 10^-2.
Sample Input
Sample Output
243.06
Test instructions is the smallest perimeter of the area that is asked to enclose all the points. But note that when there is only one point, it outputs 0.00, when there are only two points.
。 That is, the distance between two points.
。
This is the entry question of convex hull problem ... (ORZ) uses the Andrew algorithm on the Rujia white. Look at the implementation of his code: It's insane.
。
Orz.
。 It's been a long time. IQ is completely inadequate.
All right.. Because it was just touching today.
。 So one day I got this problem.
。 5555555..
。 Tears...
</pre><pre name= "code" class= "CPP" > #include <cstdio> #include <cstring> #include <iostream > #include <algorithm> #include <vector> #include <queue> #include <sstream> #include < Cmath> #define F1 (i, N) for (int. i=0; i<n; i++) #define F2 (I, M) for (int i=1; i<=m; i++) struct point{double x, y ;}; void sort (point *p, int n)//According to x from small to large (assuming x likewise, according to Y from small to large sort) {point temp; int I, J; For (i=0, i<n-1; i++) for (j=0; j<n-1-i; J + +) {if (p[j].x>p[j+1].x) { temp = P[j]; P[J] = p[j+1]; P[J+1] = temp; } if (p[j].x==p[j+1].x && p[j].y>p[j+1].y) {temp = P[j]; P[J] = p[j+1]; P[J+1] = temp; }}}int Cross (int x1, int y1, int x2, int y2)//See p[i] is in its interior: {if (x1*y2-x2*y1<=0)//cross product is less than 0. Description P[i] On the right side of the current forward direction. Hence the need to remove C[m-1],c[m-2] return 0 from the convex hull; else return 1;} int Convexhull (Point *p, point *c, int n) {int i,m=0,k; F1 (i, n)//lower convex package {while (m>1 &&!cross (c[m-2].x-c[m-1].x,c[m-2].y-c[m-1].y,c[m-1].x-p[i].x,c[m-1].y-p[ I].Y)) m--; C[m++]=p[i]; } k=m; for (i=n-2; i>=0; i--)//ask for convex package {while (m>k &&!cross (c[m-2].x-c[m-1].x,c[m-2].y-c[m-1].y,c[m-1].x-p [I].X,C[M-1].Y-P[I].Y]) m--; C[m++]=p[i]; } if (n>1) m--; return m;} Double Dis (point A, bit B)//For length between two bump points. {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} int main () {point a[105], p[105]; int n, I, M; Double lenth; while (scanf ("%d", &n) &&n) {F1 (i, N) scanf ("%lf%lf", &a[i].x, &A[I].Y); if (n==1) {printf ("0.00\n"); Continue } else if (n==2) {printf ("%.2lf\n", Dis (a[0], a[1])); Continue } sort (A, n); M=convexhull (A, p, N); lenth = 0; F2 (i, M) Lenth+=dis (P[i],p[i-1]); printf ("%.2lf\n", lenth); } return 0;}
Hdu1392:surround the Trees (convex hull problem)