Advertisement
pb_jiang

CF846C

Jun 1st, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. // Problem: C. Four Segments
  2. // Contest: Codeforces - Educational Codeforces Round 28
  3. // URL: https://codeforces.com/contest/846/problem/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifdef __DEBUG__
  13. #include "dbg.h"
  14. #else
  15. #define dbg(...) 42
  16. #endif
  17. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  18.  
  19. using ll = long long;
  20. using pii = pair<int, int>;
  21. using pll = pair<ll, ll>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24.  
  25. int main(int argc, char **argv)
  26. {
  27.     int n;
  28.     cin >> n;
  29.     vi arr(n);
  30.     for (auto &x : arr)
  31.         cin >> x;
  32.     vl acc(n + 1);
  33.     for (int i = 0; i < n; ++i)
  34.         acc[i + 1] = acc[i] + arr[i];
  35.     int a = 0, b = 0, c = 0;
  36.  
  37.     using a2l = array<ll, 2>;
  38.     vector<a2l> prev(n + 1);
  39.  
  40.     for (int i = 1; i <= n; ++i) {
  41.         if (acc[i] > prev[i - 1][0])
  42.             prev[i] = a2l{acc[i], i};
  43.         else
  44.             prev[i] = prev[i - 1];
  45.         dbg(prev[i][0], prev[i][1]);
  46.     }
  47.     ll maxv = -1e18;
  48.     ll val = 0;
  49.     dbg(val, prev[n][1], n);
  50.     ll suf = acc[n], k = n;
  51.     for (int i = n; i >= 0; --i) {
  52.         if (acc[i] > suf)
  53.             suf = acc[i], k = i;
  54.         ll t = prev[i][0] - acc[i] + suf;
  55.         if (t > maxv)
  56.             maxv = t, a = prev[i][1], b = i, c = k;
  57.     }
  58.     /*
  59.     for (int i = prev[n][1], j = n, k = n; j > 0;) {
  60.         while (j > i && val > 0) {
  61.             val = val + arr[j - 1];
  62.             if (val + prev[i][0] > maxv) {
  63.                 maxv = val + prev[i][0];
  64.                 a = i, b = j, c = k;
  65.             }
  66.             dbg(i, j, k, val);
  67.             j--;
  68.         }
  69.         if (val < 0) {
  70.             k = j;
  71.             val = 0;
  72.         }
  73.         --j;
  74.         i = prev[i - 1][1];
  75.     }
  76.     */
  77.     cout << a << ' ' << b << ' ' << c << endl;
  78.     return 0;
  79. };
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement