Portal: Surround the Trees
Test instructions: To find the perimeter of the convex hull.
Analysis: Convex hull template problem, first ordered by the pole angle, and then determine the convex hull according to the cross product positive and negative.
#include <stdio.h>#include<math.h>#include<algorithm>#include<string.h>#include<math.h>using namespacestd;Const DoubleEPS = 1e-8;Const intN = the;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; }};DoubleDist (Point A,point b) {returnsqrt (A-B) * (Ab));} Point P[n];intStack[n],top;BOOLCMP (Point p1,point p2) {DoubleTmp= (p1-p[0]) ^ (p2-p[0]); if(SGN (tmp) = =0)returnSGN (Dist (p[0],P1)-dist (p[0],P2)) <=0; returnSGN (TMP) >0;}voidGraham (intN) {Sort (P+1, p+n,cmp); stack[0]=0; stack[1]=1; Top=2; for(intI=2; i<n;i++) { while(top>1&&SGN ((p[stack[top-1]]-p[stack[top-2]]) ^ (p[i]-p[stack[top-2]]) <=0) top--; Stack[top++]=i; }}intMain () {intN; while(SCANF ("%d",&N), N) { for(intI=0; i<n;i++) {scanf ("%LF%LF",&p[i].x,&p[i].y); if(p[i].y<p[0].y| | p[i].y==p[0].y&&p[i].x<p[0].x) Swap (p[0],p[i]); } if(n==1) puts ("0.00"); Else if(n==2) printf ("%.2lf\n", Dist (p[0],p[1])); Else{Graham (n); Doubleans=0; for(intI=0; i<top;i++) ans+=dist (p[stack[i]],p[stack[(i+1)%top]]); printf ("%.2lf\n", ans); } } return 0;}
View Code
HDU 1392 (convex bag)