Hdu1392 convex hull Template

Source: Internet
Author: User

Evaluate the length of the convex hull edge

Solution: Standard convex bag GrahamAlgorithm

  # Include  <  Iostream  >  
# Include < Cmath >
# Define Pi ACOs (-1, 1.0)
Const Double EPS = 1e - 6 ;
Int SGN ( Double A ){
Return ( > EPS) - ( <- EPS );
}
Struct Point {
Double X, Y;
Point ( Double Xx = 0 , Double YY = 0 ){ // Constructor
X = XX; y = YY;
}
Point Operator - (Point B ){
Return Point (x - B. x, y - B. Y );
}
Double Operator * (Point B ){
Return (X * B. Y - Y * B. X );
}
Double Len (){
Return SQRT (x * X + Y * Y );
}
Void Input (){
Scanf ( " % Lf " , & X, & Y );
}
} P [ 102 ];
Int Stack [ 102 ], Top;
Int CMP ( Const Void * A, Const Void * B) // Return positive numbers to be exchanged in a clockwise order
{
Struct Point * C = ( Struct Point * );
Struct Point * D = ( Struct Point * ) B;
Double K = ( * C - P [ 0 ]) * ( * D - P [ 0 ]);
If (SGN (k) < 0 ) Return 1 ;
Else If (SGN (k) = 0 && SGN (( * C - P [ 0 ]). Len () - ( * D - P [ 0 ]). Len ()) > = 0 )
Return 1 ;
Else Return - 1 ;
}

Void Graham ( Int N) // Convex Hull
{
Int I;
For (I = 0 ; I <= 2 ; I ++ )
Stack [I] = I;
Top = 2 ;
For (I = 3 ; I < N; I ++ )
{
While (SGN (P [I] - P [stack [Top - 1 ]) * (P [stack [Top] - P [stack [Top - 1 ]) > 0 ){
Top -- ;
If (Top = 0 ) Break ;
}
Top ++ ;
Stack [Top] = I;
}
}

Int Main ()
{
Int N, I;
Double SUM;
While (Scanf ( " % D " , & N), n)
{
For (I = 0 ; I < N; I ++ )
P [I]. Input ();
If (N = 1 ){
Printf ( " 0.00 \ n " );
Continue ;
}
If (N = 2 ){
Printf ( " %. 2lf \ n " , (P [ 0 ] - P [ 1 ]). Len ());
Continue ;
}
Int U = 0 ;
For (I = 1 ; I < N; I ++ ) // Find the point in the lower left corner
If (P [I]. Y < P [u]. Y | (P [I]. Y = P [u]. Y && P [I]. x < P [u]. X ))
U = I;
// Swap
Point TMP = P [ 0 ];
P [ 0 ] = P [u];
P [u] = TMP;
Qsort (P + 1 , N - 1 , Sizeof (P [ 0 ]), CMP );

Graham (N );

Sum = 0 ;
For (I = 0 ; I <= Top; I ++ )
Sum + = (P [stack [I] - P [stack [(I + 1 ) % (Top + 1 )]). Len ();
Printf ( " %. 2lf \ n " , Sum );
}
Return 0 ;
}

pS; this is the same as zju1453 and foj.pdf, but 2 * (p [0]-P [1]) is output when n = 2 on zju and foj. len ().

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.