「SHOI2012」信用卡凸包

传送门

每一段圆弧直接求显然是不太好做的,但我们不难发现所有圆弧长度之和就是一个半径为 \(r\) 的圆的周长,那我们考虑只算直线段部分。

线段有两种:一种直接贴着矩形的边的,一种切于两个矩形圆角的。

第一种线段很好求,对于第二种线段画个图发现它其实就是这两段圆弧圆心的连线长度。

那么我们就直接把所有圆角矩形里的四个圆弧的圆心抠出来求个凸包周长,最后再把圆的周长加进去就好了。

参考代码:

#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;

const int _ = 1e5 + 5;
const double pi = acos(-1.0);

int N; double x, y, d, a, b, r, ans = 0;
int n, top; struct node { double x, y; } t[_], stk[_];
int cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }

node operator - (node a, node b) { return (node) { a.x - b.x, a.y - b.y }; }

double operator * (node a, node b) { return a.x * b.y - b.x * a.y; }

double dist(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }

double X(double x, double y, double d) { return x * cos(d) - y * sin(d); }

double Y(double x, double y, double d) { return x * sin(d) + y * cos(d); }

int main() {
#ifndef ONLINE_JUDGE
    freopen("cpp.in", "r", stdin), freopen("cpp.out", "w", stdout);
#endif
    scanf("%d %lf %lf %lf", &N, &b, &a, &r);
    a = a / 2 - r, b = b / 2 - r;
    for (int i = 1; i <= N; ++i) {
        scanf("%lf %lf %lf", &x, &y, &d);
        t[++n] = (node) { x + X(a, b, d), y + Y(a, b, d) };
        t[++n] = (node) { x + X(a, -b, d), y + Y(a, -b, d) };
        t[++n] = (node) { x + X(-a, b, d), y + Y(-a, b, d) };
        t[++n] = (node) { x + X(-a, -b, d), y + Y(-a, -b, d) };
    }
    sort(t + 1, t + n + 1, cmp);
    top = 0, stk[++top] = t[1], stk[++top] = t[2];
    for (int i = 3; i <= n; ++i) {
        while (top >= 2 && (stk[top] - stk[top - 1]) * (t[i] - stk[top - 1]) <= 0) --top;
        stk[++top] = t[i];
    }
    for (int i = 2; i <= top; ++i) ans += dist(stk[i], stk[i - 1]);
    top = 0, stk[++top] = t[1], stk[++top] = t[2];
    for (int i = 3; i <= n; ++i) {
        while (top >= 2 && (stk[top] - stk[top - 1]) * (t[i] - stk[top - 1]) >= 0) --top;
        stk[++top] = t[i];
    }
    for (int i = 2; i <= top; ++i) ans += dist(stk[i], stk[i - 1]);
    printf("%.2lf\n", ans + 2 * pi * r);
    return 0;
}
posted @ 2020-06-18 20:42  Sangber  阅读(217)  评论(0编辑  收藏  举报