-
Notifications
You must be signed in to change notification settings - Fork 0
/
2015-cake_cut.cpp
69 lines (54 loc) · 1.72 KB
/
2015-cake_cut.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Point {
ll x, y;
inline Point operator-(const Point& p) const { return Point{x - p.x, y - p.y}; }
inline ll operator^(const Point& p) const { return (x * p.y) - (y * p.x); }
inline Point to(const Point& p) const { return p - *this; }
};
ll area2(const int i, const int j, const int k, const vector<Point>& polygon) {
const Point& a = polygon[i % polygon.size()];
const Point& b = polygon[j % polygon.size()];
const Point& c = polygon[k % polygon.size()];
return abs(a.to(b) ^ a.to(c));
}
ll total_area2(const vector<Point>& polygon) {
ll accum = 0;
for (int i = 1; i < (int)polygon.size() - 1; i++) {
accum += area2(0, i, i + 1, polygon);
}
return accum;
}
pair<ll, ll> calculate(const vector<Point>& polygon) {
const ll total = total_area2(polygon);
ll best_piece = INT_MIN;
ll curr_piece = 0;
const auto diff = [total](const ll piece) {
ll other_piece = total - piece;
return abs(piece - other_piece);
};
for (int i = 0, j = 1; i < (int)polygon.size(); i++) {
for (;; j++) {
const ll next_piece = curr_piece + area2(i, j, j + 1, polygon);
if (diff(next_piece) >= diff(curr_piece)) {
best_piece = max({best_piece, curr_piece, total - curr_piece});
break;
}
curr_piece += area2(i, j, j + 1, polygon);
}
curr_piece -= area2(i, i + 1, j, polygon);
}
return {best_piece, total - best_piece};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
while (cin >> N) {
vector<Point> polygon(N);
for (int i = 0; i < N; i++) cin >> polygon[i].x >> polygon[i].y;
const auto [A, B] = calculate(polygon);
cout << A << " " << B << endl;
}
}