DuongNhi99

WATERMOV

Jan 6th, 2022 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ld = long double;
  5.  
  6. const int maxN = 1e6 + 5;
  7.  
  8. struct point {
  9.     ld x, y;
  10. };
  11.  
  12. bool CCW(point a, point b, point c) { // Ngược chiều kim đồng hồ
  13.     return a.x * (c.y-b.y) + b.x * (a.y-c.y) + c.x * (b.y-a.y) < 0;
  14. }
  15.  
  16. ld Stugiac(point a, point b, ld subc) {
  17.     return (b.x - a.x) * (b.y + a.y - 2 * subc);
  18. }
  19.  
  20. int n;
  21. point pot[maxN];
  22.  
  23. int main() {
  24. #ifdef LOCAL
  25.     freopen("in1.txt", "r", stdin);
  26. #else
  27.     freopen("WATERMOV.inp", "r", stdin);
  28.     freopen("WATERMOV.out", "w", stdout);
  29. #endif
  30.     ios_base::sync_with_stdio(false);
  31.     cin.tie(nullptr);
  32.  
  33.     cin >> n;
  34.     pot[1] = {0, 0};
  35.     n++;
  36.     ld sum = 0;
  37.     for (int i = 2; i <= n; i++) {
  38.         ld a; cin >> a;
  39.         sum += a;
  40.         pot[i] = {(i - 1), sum};
  41.     }
  42.  
  43.     // Convex-Hull
  44.     point p1 = pot[1], p2 = pot[n];
  45.     vector<int> down;
  46.     down.push_back(1);
  47.     for (int i = 2; i <= n; i++) {
  48.         if (i == n || CCW(p1, pot[i], p2)) {
  49.             while (down.size() >= 2 && !CCW(pot[down[down.size() - 2]], pot[down[down.size() - 1]], pot[i]))
  50.                 down.pop_back();
  51.             down.push_back(i);
  52.         }
  53.     }
  54.  
  55.     ld sall = 0, sdet = 0;
  56.     for (int i = 2; i <= n; i++)
  57.         sall += Stugiac(pot[i - 1], pot[i], pot[1].y);
  58.     for (int i = 1; i < down.size(); i++)
  59.         sdet += Stugiac(pot[down[i - 1]], pot[down[i]], pot[down[0]].y);
  60.  
  61.     sall -= sdet;
  62.     cout << fixed << setprecision(1) << sall / 2.0 << '\n';
  63.  
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment