P3829 信用卡凸包

凸包简单题

3.P3829信用卡凸包

题目描述

给若干个信用卡,信用卡的四个角都是圆角,用一根线给所有信用卡围起来要的最小长度

题目分析

因为给定的信用卡圆角半径都是相同的,那么整个图形最后圆转过的角度肯定是360o360^o其只要强制把圆角切除,然后求凸包,最后再加上一个圆角的周长即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by mrx on 2022/11/7.
//
#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <cmath>
#include <queue>
#include <array>
#include <map>
#include <iomanip>

using i64 = long long;
constexpr double eps = 1e-9;

template<typename T>
int sgn(T x) {
return x < -eps ? -1 : x > eps;
}

//int sgn(i64 x) {
// return x < 0 ? -1 : x > 0;
//}

template<typename T>
struct Point {
T x, y;

template<class Y>
Point(const Point<Y>& cp):x(cp.x), y(cp.y) {}

Point() : x(0), y(0) {}

Point(T _x, T _y) : x(_x), y(_y) {}

friend std::istream& operator >>(std::istream& is, Point& rhs) { return is >> rhs.x >> rhs.y; }

friend std::ostream& operator <<(std::ostream& os, const Point& rhs) { return os << '(' << rhs.x << ',' << rhs.y << ')'; }

Point operator -(const Point& rhs) const { return {x - rhs.x, y - rhs.y}; }

Point& operator -=(const Point& rhs) { return (*this) = (*this) - rhs; }

Point operator +(const Point& rhs) const { return {x + rhs.x, y + rhs.y}; }

Point& operator +=(const Point& rhs) { return (*this) = (*this) + rhs; }

template<class Y>
Point<double> operator *(const Y& rhs) const { return {x * rhs, y * rhs}; }

template<class Y>
Point<double> operator /(const Y& rhs) { return {x / rhs, y / rhs}; }

friend double abs(const Point& lhs) { return std::sqrt(lhs.x * lhs.x + lhs.y * lhs.y); }

friend i64 abs2(const Point& lhs) { return (lhs.x * lhs.x + lhs.y * lhs.y); }

friend T cross(const Point& lhs, const Point& rhs) { return lhs.x * rhs.y - lhs.y * rhs.x; }

friend T dot(const Point& lhs, const Point& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; }

friend double angle(const Point& rhs) { return atan2(rhs.x, rhs.y); }

Point rotate90() const { return {y, x}; }

Point<long double> rotate(double deg) { return {x * cos(deg) - y * sin(deg), x * sin(deg) + y * cos(deg)}; }

bool operator <(const Point& rhs) const { return sgn(x - rhs.x) == 0 ? sgn(y - rhs.y) < 0 : sgn(x - rhs.x) < 0; }

bool operator ==(const Point& rhs) const { return sgn(x - rhs.x) == 0 && sgn(y - rhs.y) == 0; }

bool up() const { return sgn(y) == 0 ? sgn(x) >= 0 : sgn(y) > 0; }
};

using Pl = Point<i64>;
using Pd = Point<long double>;

template<typename T>
struct Line {
Point<T> a, v;

Line(const Point<T>& a, const Point<T>& b) : a(a), v(b - a) {}

template<class Y>
Line(const Point<Y>& cp) : a(cp.a), v(cp.v) {}

Pd point(double t) {
return a + v * t;
}

friend Point<long double> intersection(const Line<T> lhs, const Line<T> rhs) {
long double t = (long double) cross(rhs.a - lhs.a, rhs.v) / cross(lhs.v, rhs.v);
return lhs.v * t + Point<double>(lhs.a);
}

double dis(const Point<T>& rhs) {
return std::abs(cross(rhs - a, v)) / v.abs();
}

Line rotate(double deg) {
Line<long double> ans(*this);
ans.v = Rotate(v, deg);
return ans;
}
};


bool isCross(Pd a, Pd b, Pd i, Pd j) {
return sgn(cross(i - a, j - i)) * sgn(cross(i - b, j - i)) == -1 && sgn(cross(b - i, a - b)) * sgn(cross(b - j, a - b)) == -1;
}

bool onSeg(Pd a, Pd i, Pd j) {
return sgn(cross(i - a, j - a)) == 0 && sgn(dot(a - i, a - j)) < 0;
}

std::vector<Pd> ConvexHull(std::vector<Pd> points) {
std::sort(points.begin(), points.end());
points.erase(std::unique(points.begin(), points.end()), points.end());

int n = points.size();
std::deque<Pd> dq;

for (auto& point: points) {
while (dq.size() > 1 && sgn(cross(dq[dq.size() - 1] - dq[dq.size() - 2], point - dq[dq.size() - 2])) <= 0)dq.pop_back();
dq.push_back(point);
}

int k = int(dq.size());
for (int i = n - 1; i >= 0; i--) {
while (dq.size() > k && sgn(cross(dq[dq.size() - 1] - dq[dq.size() - 2], points[i] - dq[dq.size() - 2])) <= 0)dq.pop_back();
dq.push_back(points[i]);
}

std::vector<Pd> ans(dq.begin(), dq.end());
return ans;
}

int dx[] = {1, 1, -1, -1};
int dy[] = {1, -1, 1, -1};

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);

int n;
std::cin >> n;
long double a, b, r;
std::cin >> b >> a >> r;
a /= 2, b /= 2;
a -= r, b -= r;

std::vector<Pd> points;
for (int i = 0; i < n; ++i) {
long double x, y, theta;
std::cin >> x >> y >> theta;
Pd center(x, y);
for (int j = 0; j < 4; ++j) {
Pd dxy = {a * dx[j], b * dy[j]};
points.push_back(center + dxy.rotate(theta));
}
}

auto rem = ConvexHull(points);
long double ans = 0;
for (int i = 1; i < rem.size(); ++i) {
ans += abs(rem[i] - rem[i - 1]);
}

ans += 2 * r * acos(-1);

std::cout << std::fixed << std::setprecision(2) << ans << '\n';
return 0;
}

P3829 信用卡凸包
https://mrxyan6.github.io/2022/11/07/P3829/
作者
mrx
发布于
2022年11月7日
许可协议