zoukankan      html  css  js  c++  java
  • POJ 1113 Wall (凸包 + 思维)

    题目:传送门

    题意:有一个 n 多边形城堡,先需在城堡外建围墙,使得围墙到城堡的距离不得小于 L,且围墙的周长最小。

    思路:答案就是凸包周长 + 半径为 L 的圆的周长。   证明

    A、B、C、D四个点,每个点都有 360 度, 然后,角1、2、3、4构成多变形的内角和为 360度,然后每个点,又要减去两个90度,那么剩下的就是圆心角5、6、7、8,它们的和为 4 * (360 - 180) - 360 = 360,刚好是一个圆。推广到任意多边形也是类似的。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    #define LL long long
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF INT_MAX
    #define inf LLONG_MAX
    #define PI acos(-1)
    using namespace std;
    
    const int N = 5e4 + 5;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
    };
    
    typedef Point Vector;
    /// 向量+向量=向量, 点+向量=向量
    Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
    ///点-点=向量
    Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
    ///向量*数=向量
    Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
    ///向量/数=向量
    Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
    
    const double eps = 1e-10;
    int dcmp(double x) {
        if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
    }
    
    bool operator < (const Point& a, const Point& b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
    
    bool operator == (const Point& a, const Point &b) {
        return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
    }
    
    double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
    double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
    double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
    double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积
    
    
    Point P[N], Q[N];
    
    int ConvexHull(Point* p, int n, Point* ch) {
        sort(p, p + n);
        int m = 0;
        rep(i, 0, n - 1) {
            while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        int k = m;
        dep(i, 0, n - 2) {
            while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        if(n > 1) m--;
        return m;
    }
    
    int main() {
        int n, L;
        scanf("%d %d", &n, &L);
        rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
        n = ConvexHull(P, n, Q);
        double ans = 0;
        rep(i, 0, n - 1) {
            ans += Length(Q[(i + 1) % n] - Q[i]);
        }
    
        ans += 2.0 * PI * 1.0 * L;
    
        printf("%d
    ", (int)(ans + 0.5));
    
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    CentOS之文件搜索命令locate
    CentOs之链接命令
    CentOs之常见目录作用介绍
    centOs之目录处理命令
    Query注解及方法限制
    Repository接口
    OkHttp和Volley对比
    Base64加密与MD5的区别?
    支付宝集成
    Android 中 非对称(RSA)加密和对称(AES)加密
  • 原文地址:https://www.cnblogs.com/Willems/p/12409122.html
Copyright © 2011-2022 走看看