There 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
This problem is convex hull template.
Http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <cmath>6 using namespacestd;7 Const Doubleeps=1e-Ten;8 Const intn= the;9 structpoint{Ten Doublex, y; OnePoint (Doublex_=0,Doubley_=0) {x=x_;y=Y_;} AFriend Pointoperator-(Point A,point b) { - returnPoint (a.x-b.x,a.y-b.y); - } the }p[n],st[n]; - DoubleSqrDoublex) {returnx*x;} - DoubleDis (point A,point b) {returnsqrt (SQR (a.x-b.x) +SQR (a.y-b.y));} - DoubleCross (point A,point b) {returna.x*b.y-a.y*b.x;} + BOOLCMP (point A,point b) { - DoubleS=cross (a-p[0],b-p[0]); + if(Fabs (s) >=eps)returns>=EPS; A returnDis (a,p[0]) <dis (b,p[0]); at } - intN,t,top; - Doubleans; - intMain () { - while(SCANF ("%d", &n)!=eof&&N) { - for(intI=0; i<n;i++) inscanf"%LF%LF",&p[i].x,&p[i].y); - if(n==1){ toPuts"0.00"); + Continue; - } the if(n==2){ *printf"%.2f\n", Dis (p[0],p[1])); $ Continue;Panax Notoginseng } -t=0; the for(intI=1; i<n;i++)if(p[i].y<p[t].y| | p[i].y==p[t].y&&p[i].x<p[t].x) t=i; + if(t) swap (p[0],p[t]); ASort (p+1, p+n,cmp); thep[n]=p[0];top=0; +st[++top]=p[0]; -st[++top]=p[1]; $st[++top]=p[2]; $ for(intI=3; i<=n;i++){ - while(top>=2&&cross (st[top]-p[i],st[top-1]-p[i]) >=0) top--; -st[++top]=P[i]; the } -ans=0;Wuyi for(intI=2; i<=top;i++) Ans+=dis (st[i],st[i-1]); theprintf"%.2f\n", ans); - } Wu return 0; -}
Computational geometry (convex hull template): HDU 1392