[P2742] surrounded by cows

Well the Buddha school system more, the other CSP is not far away

If that, you can easily find articles blog to learn something. .

This, to the effect that: a package to go lower half of the observed assigned to the rightmost point slope fenced monotonically increases from the leftmost point. Similar upper part

Think about it should not be difficult to achieve (it), won half as an example: First, left to right, from bottom to top ordering them to the point, get a stack to save their point, the goal is to make the last point in the stack all It is lower / upper portion of the pocket desired point. Began to put a stack took the first two points, then began to add:

If the new point and added into the stack slope greater than the slope of the end point and the second end of the stack point reciprocal points (FIG, red is the point stack), i.e., formed to surround situation, the new point directly to the stack.

(Let the black spots into the stack

 

Otherwise: As shown, into the newly added points (4) and the point end of the stack (3) than the slope of the point end of the stack (3) and the second point (2) the inverse of the slope of the small

 

 It is not surrounded by the situation. At this point kicked tris (it is not a point on the package):

 

Check again, added to the list of newly found points (4) and the point end of the stack (2) is smaller than the slope of the point end of the stack (2) and a second point (1) the inverse of the slope is not surrounded posture, so repeat the above operation, kicked two points:

 

 At this point there was nothing to check, and the 4-point stack, and began to raise a point under the direct.

Repeat this until all points have been kicked out of a stack of the operating point and does not meet the conditions during. Finally, only the lower half of the points in the sub-stack. One out and count the distance.

Similar to the upper half of it.

Then note that the package is composed of two distance between two points * 2, to be fenced thing. Do not do those weird force paper-cut no problem

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<cmath>
#define MAXN 1000001
#define spc 1010101010 //特判k不存在 
using namespace std;
int N;
double ans;
struct cows{double x,y;}cow[10001];
stack<cows>zhan;

inline double caldis(cows a, cows b){return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2 ));} // card often by nature 
inline Double Slope (a Cows, Cows b) { return BX == AX SPC:? (AY-by) / (BX-AX);} // calculate a transmission to b the slope (slope of the slope is no problem 
BOOL CMP (a Cows, Cows B) { return AX AY BX == <by: AX <? BX;} 

int main () { 
    CIN >> N;
     for ( int I = . 1 ; I <= N; I ++ ) 
    CIN >> Cow [I] .x >> Cow [I] .y; 
    
    IF (N <= . 1 ) {the printf ( " 0.00 " ); return  0 ;}
    IF (N == 2 ) {ANS = 2 * caldis (Cow [ . 1 ], Cow [ 2 ]); the printf ( " % .2lf " , ANS); return  0 ;} 
    Sort (Cow + . 1 , Cow + . 1 + N , cmp); 
    
    // lower half now on the search package. 
    zhan.push (Cow [ . 1 ]); 
    zhan.push (Cow [ 2 ]);
     int J = . 3 ;
     Double K = Slope (Cow [ . 1 ], Cow [ 2 ]);
     the while (J <= N) 
    { 
        Cows c= Zhan.top (); 
        
        the while (Slope (C, Cow [J]) < K) 
        { 
            zhan.pop (); 
            C = zhan.top (); 
            zhan.pop (); 
            IF (zhan.empty ()) { 
                zhan.push (C); 
                K = Slope (C, Cow [J]);
                 Continue ; 
            } 
            K = Slope (zhan.top (), C); 
            zhan.push (C); 
        } 
        zhan.push (Cow [ J]); 
        K = Slope (C, Cow [J]); 
        J ++ ; 
    } 
    // now to the lower half of the circumferential length count

    cows nowcow=zhan.top();
    zhan.pop();
    while(!zhan.empty())
    {
        ans+=caldis(nowcow,zhan.top());
        nowcow=zhan.top();
        zhan.pop();
    }
    //现在开始搜包的上半边。
    zhan.push(cow[1]);
    zhan.push(cow[2]);
    j=3;
    k=slope(cow[1],cow[2]);
    while(j<=N)
    {
        cows c=zhan.top();
        
        while(Slope (C, Cow [J])> K) 
        { 
            zhan.pop (); 
            C = zhan.top (); 
            zhan.pop (); 
            IF (zhan.empty ()) { 
                zhan.push (C); 
                K = Slope (C, Cow [J]);
                 Continue ; 
            } 
            K = Slope (zhan.top (), C); 
            zhan.push (C); 
        } 
        zhan.push (Cow [J]); 
        K = Slope ( C, Cow [J]); 
        J ++ ; 
    } 
    // now to half the circumference of the count 

    nowcow =zhan.top();
    zhan.pop();
    while(!zhan.empty())
    {
        ans+=caldis(nowcow,zhan.top());
        nowcow=zhan.top();
        zhan.pop();
    }
    printf("%.2lf",ans);
}

 

Guess you like

Origin www.cnblogs.com/snowysniper/p/11535639.html