Test instructions
Surround the Trees |
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) |
Total submission (s): 209 Accepted Submission (s): 104 |
|
problem Descriptionthere is a lot of trees in the 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. |
Output The 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 |
|
Sourceasia 1997, Shanghai (Mainland China) |
Recommendignatius.l |
Topic Analysis:
To find the perimeter of the convex hull. The problem is about the same as getting wall. It is important to note that the number of nodes in this problem may be <=2, which needs to be dealt with separately.
The code is as follows:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm>using namespace std; Const double EPSI = 1e-8;const double pi = ACOs ( -1.0); const int MAXN = 101;struct ppoint{//struct Try not to define it as a point, easy and variable in C + + itself The same name double x;double y; PPoint (Double _x = 0,double _y = 0): X (_x), Y (_y) {}ppoint operator-(const ppoint& OP2) Const{return ppoint (x-op2.x,y -OP2.Y);} Double operator^ (const ppoint &OP2) Const{return x*op2.y-y*op2.x;}; inline int sign (const double &x) {if (x > Epsi) {return 1;} if (x <-epsi) {return-1;} return 0;} Inline double sqr (const double &x) {return x*x;} Inline double mul (const ppoint& p0,const ppoint& p1,const ppoint& p2) {return (p1-p0) ^ (p2-p0);} Inline double dis2 (const ppoint &p0,const ppoint &p1) {return Sqr (p0.x-p1.x) + SQR (P0.Y-P1.Y);} inline double dis (const ppoint& p0,const ppoint& p1) {return sqrt (Dis2 (P0,P1));} int n; PPoint P[MAXN]; PPoint convex_hull_p0;inline bool convex_hull_cmp (const PPoint& A,const ppoint& b) {return sign (Mul (convex_hull_p0,a,b) >0) | | (Sign (Mul (convex_hull_p0,a,b)) = = 0 && dis2 (convex_hull_p0,a) < Dis2 (convex_hull_p0,b));} int Convex_hull (ppoint* a,int n,ppoint* b) {int i;for (i = 1; i < n; ++i) {if (sign (a[i].x-a[0].x) < 0 | | (sign (a[i].x-a[0].x) = = 0 && sign (A[I].Y-A[0].Y) < 0)) {swap (a[i],a[0]);}} Convex_hull_p0 = a[0];//These two lines of code do not change sequentially. Otherwise it will Wasort (A,A+N,CONVEX_HULL_CMP), b[0] = a[0];b[1] = A[1];int newn = 2;for (i = 2; i < n; ++i) {while (Newn > 1 && Amp Sign (Mul (b[newn-1],b[newn-2],a[i)) >= 0) {newn--;} b[newn++] = A[i];} return newn;} int main () {while (scanf ("%d", &n)!=eof,n) {int i;for (i = 0; i < n; ++i) {scanf ("%lf%lf", &p[i].x,&p[i].y);} /** * It is important to note that there may be a situation where the node number <= 2 is present in the problem. * While Graham calculates the convex hull algorithm only applies to the case of node number >=3. * So the number of nodes <=2 need to do a bit of processing ... */if (n < 2) {//If the number of nodes <2printf ("0.00\n"),//Direct output 0.00continue;//for the next cycle}if (n = = 2) {// If the node = = = 2printf ("%.2lf\n", Dis (p[0],p[1])), the distance between p[0] and p[1] can be calculated directly.continue;//the next cycle}n = Convex_hull (p,n,p);p [n] = p[0];//. Point to the beginning of the last point double ans = 0;for (i = 0; i < n; ++i) {//Calculate the week of the convex hull Long ans + = dis (p[i],p[i+1]);} printf ("%.2lf\n", ans);} return 0;}
(Hdu 7.1.4) Surround the Trees (for ungrateful circumference-calculates the perimeter of the smallest convex polygon that encloses all points)