HDU 1392 (convex bag)

Source: Internet
Author: User

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&AMP;&AMP;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)

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.