UVA10652 Board Wrapping

凸包板子题

1.UVA10652 Board Wrapping

题目描述

nn 个矩形木条,你需要用一个面积尽可能小的凸多边形把它们围起来,并计算出木条占整个包装面积的百分比。

题目分析

通过旋转公式求出来所有矩形的顶点,然后通过这个求出来凸包,这个凸包就是围起来的最小多边形。直接算两个的面积就行。

代码

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Created by mrx on 2022/10/30.
//
#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-6;

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

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

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

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

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

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) {
x += rhs.x;
y += rhs.y;
return *this;
}

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

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

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

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

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

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

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

friend double abs(const Point& lhs) {
return std::sqrt(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;
}

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

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

bool operator ==(const Point& rhs) const {
return std::abs(x - rhs.x) <= eps && std::abs(y - rhs.y) <= eps;
}
};

template<typename T>
Point<long double> Rotate(Point<T> a, double deg) { return {a.x * cos(deg) - a.y * sin(deg), a.x * sin(deg) + a.y * cos(deg)}; }

using Pl = Point<i64>;
using Pd = Point<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;
}
};

using Pd = Point<double>;
using Ld = Line<double>;

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;
}

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

std::vector<Pd> ConvexHull(std::vector<Pd> points) {
int n = points.size();
std::sort(points.begin(), points.end());
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;
}

void sol() {
int n;
std::cin >> n;

std::vector<Pd> points;
double fz = 0;
for (int i = 0; i < n; ++i) {
double x, y, w, h, phi;
std::cin >> x >> y >> w >> h >> phi;
fz += w * h;
Pd cent(x, y);
for (int j = 0; j < 4; ++j) {
Pd vertex = cent + Pd(w / 2 * dx[j], h / 2 * dy[j]).rotate(-phi / 180 * acos(-1));
points.push_back(vertex);
}
}

// for (auto x: points)std::cout << x << '\n';
std::vector<Pd> convexHull = ConvexHull(points);
int m = convexHull.size();
// for (auto x: convexHull)std::cout << x << '\n';
double fm = 0;
for (int i = 0; i < m; ++i) {
fm -= cross(convexHull[i] - convexHull[(i + m - 1) % m], convexHull[i] - Pd(0, 0)) / 2;
}
// std::cerr << fz << ' ' << fm << '\n';
// std::cerr << fz / fm << '\n';
double ans = fz / fm * 100;
std::cout << std::fixed << std::setprecision(1)<<ans<<" %\n";
}

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
sol();
}
return 0;
}

UVA10652 Board Wrapping
https://mrxyan6.github.io/2022/10/30/UVA10652/
作者
mrx
发布于
2022年10月30日
许可协议