-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2191 - Polygon Area.cpp
49 lines (42 loc) · 978 Bytes
/
2191 - Polygon Area.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct Point {
ll x, y;
void read() { cin >> x >> y; }
Point operator-(const Point &b) const { return Point({x - b.x, y - b.y}); }
ll operator*(const Point &b) const { return x * b.y - y * b.x; }
ll cross(const Point &b, const Point &c) {
return (b - *this) * (c - *this);
}
};
void solve() {
ll n;
cin >> n;
vector<Point> a(n);
vector<bool> b(n, false);
for (auto &i : a) i.read();
auto next = [&](int idx) {
int i = (idx + 1) % n;
while (b[i]) i++, i %= n;
return i;
};
int x = 0, y = 1, z = 2;
ll ans = 0;
for (int count = 0; n - count >= 3; count++) {
b[y] = true;
ans += (a[y].cross(a[x], a[z]));
x = z;
y = next(z);
z = next(y);
}
cout << abs(ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}