-
Notifications
You must be signed in to change notification settings - Fork 0
/
1336-garden_fence.cpp
86 lines (64 loc) · 2.23 KB
/
1336-garden_fence.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
enum PointType { PINE = 0, LARCH = 1 };
struct Point {
ll x, y;
PointType type;
int value;
Point move_to_upper() const {
if (is_above()) return *this;
return {-x, -y, type, value};
}
Point operator-(const Point& p) const { return {x - p.x, y - p.y, type, value}; }
ll operator^(const Point& p) const { return x * p.y - y * p.x; }
bool operator==(const Point& p) const { return x == p.x && y == p.y; }
bool is_above() const { return y > 0 || (y == 0 && x > 0); }
};
bool cmp(const Point& p, const Point& q) {
return (p.move_to_upper() ^ q.move_to_upper()) > 0;
}
int calculate_loss(vector<Point>& points, PointType above_type) {
const PointType below_type = above_type == LARCH ? PINE : LARCH;
array<int, 2> destroyed = {0, 0};
for (const Point& p : points) {
if (p.is_above() && p.type == below_type) destroyed[p.type] += p.value;
if (!p.is_above() && p.type == above_type) destroyed[p.type] += p.value;
}
int ans = destroyed[0] + destroyed[1];
for (int i = 0; i < (int)points.size(); i++) {
const Point p = points[i];
const Point q = points[(i + 1) % points.size()];
if (p.is_above())
destroyed[p.type] += p.type == above_type ? p.value : -p.value;
else
destroyed[p.type] += p.type == below_type ? p.value : -p.value;
if ((p ^ q) != 0) ans = min(ans, destroyed[0] + destroyed[1]);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int P, L;
while (cin >> P >> L && P && L) {
vector<Point> points(P + L);
for (int i = 0; i < P + L; i++) {
cin >> points[i].x >> points[i].y >> points[i].value;
points[i].type = i < P ? PINE : LARCH;
}
int ans = INT_MAX;
for (const Point& center : points) {
vector<Point> centered_points;
for (const Point& p : points) {
if (p == center) continue;
centered_points.push_back(p - center);
}
sort(centered_points.begin(), centered_points.end(), cmp);
const int loss_pine = calculate_loss(centered_points, PINE);
const int loss_larch = calculate_loss(centered_points, LARCH);
ans = min({ans, loss_pine, loss_larch});
}
cout << ans << '\n';
}
}