Surround the trees
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7203 accepted submission (s): 2752
Problem descriptionthere 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.
Inputthe 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.
Outputthe minimal length of the rope. The precision shocould 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
Horizontally ordered Andrew algorithm:
# Include <iostream> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; struct node {Double X, Y;} A [105], B [105]; double CMP (node N, node m) // compare the X coordinate first, and compare the Y coordinate (from small to large) {If (N. x! = M. x) return n. x <m. x; elsereturn. Y <m. y;} double cross (node A, Node B, node c) // calculate the size of the Cross. {return (B. x-a.x) * (C. y-a.y)-(C. x-a.x) * (B. y-a.y);} double DIS (node A, Node B) // calculates the distance {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} int CH (node * a, int N, node * B) {sort (A, A + N, CMP); int m = 0, I; for (I = 0; I <n; I ++) // from left to right, select the bottom boundary {While (M> 1 & cross (B [m-2], B M-1], a [I]) <0) m --; B [M ++] = A [I];} int K = m; for (I = n-2; i> = 0; I --) // from right to left, select the upper boundary {While (M> K & cross (B [m-2], B [s-1], A [I]) <0) m --; B [M ++] = A [I];} If (n> 1) m --; return m ;} int main () {int N; while (CIN> N) {If (n = 0) break; memset (A, 0, sizeof ()); memset (B, 0, sizeof (B); int I, j; for (I = 0; I <n; I ++) {CIN> A [I]. x> A [I]. y;} // cout <CH (A, N, B) <Endl; // output the total number of selected points if (n = 1) cout <0.00 <Endl; else if (n = 2) printf ("%. 2lf \ n ", DIS (A [0], a [1]); else {int M = CH (A, N, B); double S = 0; for (I = 1; I <m; I ++) S + = DIS (B [I-1], B [I]); S + = DIS (B [0], B [M-1]); printf ("%. 2lf \ n ", S) ;}// for (I = 0; I <CH (A, N, B); I ++) // output the coordinates of the selected point // cout <B [I]. x <"" <B [I]. Y <Endl;} return 0 ;}