• Surround the Trees---hdu1392(凸包GraHam模板)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392

    题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度;

    就是求凸包的周长的,把凸包各边的长度加起来就好了;注意n<=2的情况,运用GraHam算法,时间复杂度是O(nlogn);

    GraHam算法的过程:

    #include <stdio.h>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    using namespace std;
    #define met(a, b) memset(a, b, sizeof(a))
    const double eps = 1e-10;
    const double PI = acos(-1);
    const int N = 150010;
    
    struct point
    {
        double x, y;
        point(double x=0, double y=0) : x(x), y(y){}
        friend point operator - (const point& p1, const point& p2)
        {
            return point(p1.x-p2.x, p1.y-p2.y);
        }
        friend double operator ^ (const point& p1, const point& p2)
        {
            return p1.x*p2.y - p1.y*p2.x;
        }
    }p[N], res[N];
    
    double Dist(point p1, point p2)
    {
        double dx = p1.x - p2.x, dy = p1.y - p2.y;
        return sqrt(dx*dx + dy*dy);
    }
    bool cmp1(point p1, point p2)
    {
        if(p1.y == p2.y)
            return p1.x < p2.x;
        return p1.y < p2.y;
    }
    bool cmp2(point p1, point p2)///极角排序;若极角相同,距离近的在前面;
    {
        double k = (p1-p[0])^(p2-p[0]);
        if( k>eps || (fabs(k)<eps && Dist(p1, p[0]) < Dist(p2, p[0]) ))
            return 1;
        return 0;
    }
    int Graham(int n)///返回凸包的点的个数;
    {
        res[0] = p[0];if(n == 1) return 1;
        res[1] = p[1];if(n == 2) return 2;
        int top = 1;
        for(int i=2; i<n; i++)
        {
            while(top && ((res[top]-res[top-1])^(p[i]-res[top-1])) <= 0) top--;
            res[++top] = p[i];
        }
        return top+1;
    }
    
    int main()
    {
        int n;
        while(scanf("%d", &n), n)
        {
            for(int i=0; i<n; i++)
                scanf("%lf %lf", &p[i].x, &p[i].y);
    
            if(n == 0 || n == 1)
            {
                printf("0
    ");
                continue;
            }
            if(n == 2)
            {
                printf("%.2f
    ", Dist(p[1], p[0]));
                continue;
            }
    
            sort(p, p+n, cmp1);///p[0]为最下方靠左的点;
            sort(p+1, p+n, cmp2);///以p[0]为基点,按叉积进行排序;
    
            int cnt = Graham(n);///求凸包的顶点个数cnt+1,保存在res中,下标从0开始;
    
            double ans = Dist(res[0], res[cnt-1]);
            for(int i=1; i<cnt; i++)
                ans += Dist(res[i], res[i-1]);
    
            printf("%.2f
    ", ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    数据结构与算法(3-4)--矩阵的压缩存储
    数据结构与算法(3-3)--队列的应用
    数据结构与算法(3-2)--栈的应用
    数据结构与算法(3-1)--栈和队列
    数据结构与算法(2)--线性表(数组和链表)
    数据结构与算法(1)--时间及空间复杂度
    python变量与地址的关系
    python高级(03)--socket编程
    python高级(02)--生成器和迭代器
    python处理http接口请求
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5906007.html
Copyright © 2020-2023  润新知