HDU 1392 Surround the Trees

二次联通门 : HDU 1392 Surround the Trees

 

 

 

 

/*
    HDU 1392 Surround the Trees
 
    凸包模板题
    做出凸包后
    
    从凸包上的第一个点开始
    向后扫
    计算两点间距离
    累加即可 
*/
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>

#define EPS 1e-7

void read (int &now)
{
    register char word = getchar ();
    int temp = 0;
    for (now = 0; !isdigit (word); word = getchar ())
        if (word == '-')
            temp = 1;
    for (; isdigit (word); now = now * 10 + word - '0', word = getchar ());
    if (temp)
        now = -now;
}

struct Point
{
    int x, y;
    
    Point (int __x, int __y) : x (__x), y (__y) {}
    Point () {}

    bool operator < (const Point &now) const
    {
        return this->x == now.x ? this->y < now.y : this->x < now.x;
    }
};

inline Point operator - (const Point &A, const Point &B)
{
    return Point (A.x - B.x, A.y - B.y); 
}

#define Max 1000

inline double Abs (double now)
{
    return now < 0 ? -now : now;
}

inline int Cmp (double now)
{
    if (Abs (now) <= EPS) 
        return 0;
    return now > 0 ? 1 : -1;
}

inline int Cross (const Point A, const Point B)
{
    return A.x * B.y - A.y * B.x;
}

int Make_Honvex_Hull (Point *point, int N, Point *Stack)
{
    std :: sort (point + 1, point + 1 + N);
    int top = 0;
    register int i;
    
    for (i = 1; i <= N; ++ i)
    {
        for (; top > 1 && Cmp (Cross (Stack[top] - Stack[top - 1], point[i] - Stack[top - 1])) == -1; -- top);
        Stack[++ top] = point[i];
    }
    int k = top;
    for (i = N - 1; i >= 1; -- i)
    {
        for (; top > k && Cmp (Cross (Stack[top] - Stack[top - 1], point[i] - Stack[top - 1])) == -1; -- top);
        Stack[++ top] = point[i];
    }
    return top;
}


inline double Mul_two_point_distance (const Point &A, const Point &B)
{
    return sqrt ((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}

double Mul_Polygon_length (Point *point, const int N)
{
    double res = 0.0;
    
    for (int i = 1; i < N; i ++)
        res += Mul_two_point_distance (point[i], point[i + 1]);

    return res;
}

Point point[Max], List[Max];
int main (int argc, char *argv[])
{
    int N;
    register int i;
    for (scanf ("%d", &N); N; scanf ("%d", &N))
    {        
        for (i = 1; i <= N; i ++)
            read (point[i].x), read (point[i].y);
        
        int M = Make_Honvex_Hull (point, N, List);
        double Answer = Mul_Polygon_length (List, M);
        
        printf ("%.2lf\n", N == 2 ? Answer / 2 : Answer);
    }
    
    return 0;
}

 

posted @ 2017-08-09 17:49  ZlycerQan  阅读(208)  评论(0编辑  收藏  举报