码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1113 Wall (凸包 + 思维)

时间:2020-03-04 14:21:13      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:max   fabs   就是   mat   string   mem   sizeof   abs   clu   

题目:传送门

题意:有一个 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\n", (int)(ans + 0.5));

    return 0;
}

 

POJ 1113 Wall (凸包 + 思维)

标签:max   fabs   就是   mat   string   mem   sizeof   abs   clu   

原文地址:https://www.cnblogs.com/Willems/p/12409122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!