Surround the Trees
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 Input9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
Sample Output243.06
Test instructions
Simple convex hull Template title
Exercises
Here is an algorithm to find convex hull: Graham. (There may be some discrepancies relative to other people's interpretations, but the idea of the algorithm is largely the same, which solves the convex hull problem)
Compared to the n*m time of the parcel method, the Graham algorithm has a great increase in time, as long as the N*log (n) time is enough. Its basic idea is as follows:
1, first, put all the points in accordance with the Y minimum priority, followed by X small priority order
2, maintain a stack, with the cross product of the vector to determine the new insertion point with the top of the stack of the point which in the periphery, if the top point of the stack at the current point of insertion to the left, then the top of the stack of this element pop-up, after the popup can not continue to insert the next point, to continue to determine the When the currently inserted point is to the left of the point at the top of the stack, you can press the point you want to insert into the stack and go to the next point.
http://blog.csdn.net/bone_ace/article/details/46239187
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd;Const intN = 1e5+Ten, M =30005, mod = 1e9 +7, INF =0x3f3f3f3f; typedefLong Longll;structpoint{Doublex, y; Point (Doublex =0,Doubley =0): X (x), Y (y) {} friend pointoperator+(Point A,point b) {returnPoint (a.x+b.x,a.y+b.y); } Friend Pointoperator-(Point A,point b) {returnPoint (a.x-b.x,a.y-b.y); }}p[n],res[n];DoubleDis (point a,point b) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}Doubledot (point A,point b) {returna.x*b.y-b.x*a.y;}intCMP (point A,point b) {if(A.Y==B.Y)returna.x<b.x; Else returna.y<b.y;}intGraham (point* p,intn,point*Res) {Sort (P+1, p+n+1, CMP); res[1] = p[1]; res[2] = p[2]; inttop =2, Len; for(intI=3; i<=n;i++) { while(top>=2&& dot (p[i]-res[top-1],res[top]-res[top-1]) >=0) top--; res[++top] =P[i]; } Len=top; for(inti=n;i>=1; i--) { while(Top!=len&&dot (p[i]-res[top-1],res[top]-res[top-1]) >=0) top--; res[++top] =P[i]; } returntop;}intMain () {intN; while(SCANF ("%d", &n) &&N) { for(intI=1; i<=n;i++) scanf ("%LF%LF",&p[i].x,&p[i].y); if(n==1) {printf ("0.00\n");Continue; } if(n==2) {printf ("%.2f\n", Dis (p[1],p[n])); Continue; } intm=Graham (p,n,res); Doubletot=0; for(intI=2; i<=m;i++) Tot+=dis (res[i-1],res[i]); printf ("%.2f\n", tot); }}
HDU 1392 Convex bun