Surround the Trees (convex bag)

Source: Internet
Author: User

Surround the Trees
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 291 Accepted Submission (s): 140
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.
Input the 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 of the minimal length of the rope. The precision should 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
Sourceasia 1997, Shanghai (Mainland China)
Recommendignatius.l

/*
You need a special sentence when you're on. Especially when it comes to n==2, it is the distance between the returns, not twice times the distance.
*/
#include <bits/stdc++.h>using namespacestd;Const DoubleEPS = 1e-8;Const DoublePI = ACOs (-1.0);intSgnDoublex) { if(Fabs (x) < EPS)return 0; if(X <0)return-1; Else return 1;}structpoint{Doublex, y; Point () {}, point (Double_x,Double_y) {x= _x;y =_y; } Pointoperator-(ConstPoint &b)Const { returnPoint (X-b.x,y-b.y); } //Cross Product Double operator^(ConstPoint &b)Const { returnx*b.y-y*b.x; } //dot Product Double operator*(ConstPoint &b)Const { returnx*b.x + y*b.y; } voidinput () {scanf ("%LF%LF",&x,&y); }};structLine {point s,e; Line () {} line (point _s,point _e) {s= _s; E =_e; }}; //* Distance between two pointsDoubleDist (Point A,point b) {returnsqrt (A-B) * (Ab));} /** Find convex hull, Graham algorithm * point number 0~n-1* return convex hull result Stack[0~top-1] is the number of the convex hull*/Const intMAXN =1010; Point LIST[MAXN];intStack[maxn],top;//Polar Order relative to list[0]BOOL_cmp (Point p1,point p2) {DoubleTMP = (p1-list[0]) ^ (p2-list[0]); if(SGN (TMP) >0) return true; Else if(SGN (tmp) = =0&& sgn (Dist (p1,list[0])-Dist (p2,list[0])) <=0) return true; Else return false;}voidGraham (intN) {Point p0; intK =0; P0= list[0]; //find the bottom one . for(inti =1; I < n;i++) { if((P0.y > list[i].y) | | (P0.y = = List[i].y && p0.x >list[i].x)) {P0=List[i]; K=i; }} swap (list[k],list[0]); Sort (List+1, list+n,_cmp); if(n = =1) {Top=1; stack[0] =0; return; } if(n = =2) {Top=2; stack[0] =0; stack[1] =1; return ; } stack[0] =0; stack[1] =1; Top=2; for(inti =2; I < n;i++) { while(Top >1&& SGN (list[stack[top-1]]-list[stack[top-2]]) ^ (list[i]-list[stack[top-2]]) <=0) Top--; Stack[top++] =i; }} intN;intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%d", &n)!=eof&&N) { for(intI=0; i<n;i++) {list[i].input (); } if(n==1) {printf ("0.00\n"); Continue; }Else if(n==2) {printf ("%.2lf\n", Dist (list[0],list[1])); Continue; } Graham (n); DoubleCur=0; for(intI=0; i<top;i++) {cur+=dist (list[stack[i%top]],list[stack[(i+1)%top]]); } printf ("%.2lf\n", cur); } return 0;}

Surround the Trees (convex bag)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.