Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ld = long double;
- const int maxN = 1e6 + 5;
- struct point {
- ld x, y;
- };
- bool CCW(point a, point b, point c) { // Ngược chiều kim đồng hồ
- return a.x * (c.y-b.y) + b.x * (a.y-c.y) + c.x * (b.y-a.y) < 0;
- }
- ld Stugiac(point a, point b, ld subc) {
- return (b.x - a.x) * (b.y + a.y - 2 * subc);
- }
- int n;
- point pot[maxN];
- int main() {
- #ifdef LOCAL
- freopen("in1.txt", "r", stdin);
- #else
- freopen("WATERMOV.inp", "r", stdin);
- freopen("WATERMOV.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n;
- pot[1] = {0, 0};
- n++;
- ld sum = 0;
- for (int i = 2; i <= n; i++) {
- ld a; cin >> a;
- sum += a;
- pot[i] = {(i - 1), sum};
- }
- // Convex-Hull
- point p1 = pot[1], p2 = pot[n];
- vector<int> down;
- down.push_back(1);
- for (int i = 2; i <= n; i++) {
- if (i == n || CCW(p1, pot[i], p2)) {
- while (down.size() >= 2 && !CCW(pot[down[down.size() - 2]], pot[down[down.size() - 1]], pot[i]))
- down.pop_back();
- down.push_back(i);
- }
- }
- ld sall = 0, sdet = 0;
- for (int i = 2; i <= n; i++)
- sall += Stugiac(pot[i - 1], pot[i], pot[1].y);
- for (int i = 1; i < down.size(); i++)
- sdet += Stugiac(pot[down[i - 1]], pot[down[i]], pot[down[0]].y);
- sall -= sdet;
- cout << fixed << setprecision(1) << sall / 2.0 << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment