HDU 1392 surround the trees

Source: Internet
Author: User

Hdu_1392

When N = 2, you only need to output the distance between two points, instead of twice the output distance.

As to why we can find the perimeter of a convex bag, we may use the reverse proof method to prove that the rope on the convex bag must pass through, at the same time, we cannot find a better result so that a part of the rope is not on the side length of the convex bag. Therefore, we only need to obtain the convex hull and calculate its perimeter.

I learned Graham.AlgorithmLater debut.

# Include <stdio. h>
# Include < String . H>
# Include <math. h>
# Include <stdlib. h>
# Define Maxd 110
# Define Zero 1e-8
Struct Point
{
Double X, Y;
} P [maxd], Res [maxd];
Int N, P;
Int DCMP ( Double X)
{
Return FABS (x) <zero? 0 : (X < 0 ? - 1 :1 );
}
Double Det ( Double X1, Double Y1, Double X2, Double Y2)
{
Return X1 * Y2-X2 * Y1;
}
Int CMP ( Const Void * _ P, Const Void * _ Q)
{
Point * P = (point *) _ p, * q = (point *) _ q;
If (P-> Y = Q-> Y)
Return P-> x <q-> X? - 1 : 1 ;
Return P-> Y <q-> Y? - 1 : 1 ;
}
Double Sqr ( Double X)
{
Return X * X;
}
Void Init ()
{
Int I, J, K;
For (I = 0 ; I <n; I ++)
Scanf ( " % Lf " , & P [I]. X, & P [I]. y );
Qsort (p, n, Sizeof (P [0 ]), CMP );
}
Int Del ( Int Top, Int I)
{
If (DCMP (det (RES [Top]. X-res [Top- 1 ]. X, Res [Top]. Y-res [Top- 1 ]. Y, P [I]. X-res [Top]. X, P [I]. Y-res [Top]. y) < 0 )
Return 1 ;
Return 0 ;
}
Int Graham ()
{
Int I, J, K, mint, Top = 1 ;
Res [ 0 ] = P [ 0 ], Res [ 1 ] = P [ 1 ];
For (I = 2 ; I <n; I ++)
{
While (Top & del (top, I ))
-- Top;
Res [++ top] = P [I];
}
Mint = top;
Res [++ top] = P [n- 2 ];
For (I = N- 3 ; I> = 0 ; I --)
{
While (Top! = Mint & del (top, I ))
-- Top;
Res [++ top] = P [I];
}
Return Top;
}
Void Solve ()
{
Int I;
Double Ans;
If (N = 1 )
Printf ( " 0.00 \ n " );
Else If (N = 2 )
Printf ( " %. 2lf \ n " , SQRT (sqr (P [ 0 ]. X-P [ 1 ]. X) + sqr (P [ 0 ]. Y-P [ 1 ]. Y )));
Else
{
P = Graham ();
Ans = 0 ;
For (I = 0 ; I <p; I ++)
Ans + = SQRT (sqr (RES [I]. X-res [I + 1 ]. X) + sqr (RES [I]. Y-res [I + 1 ]. Y ));
Printf ( " %. 2lf \ n " , ANS );
}
}
Int Main ()
{
For (;;)
{
Scanf (" % D " , & N );
If (! N)
Break ;
Init ();
Solve ();
}
Return 0 ;
}


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.