Advertisement
pb_jiang

CF846C

Jun 1st, 2023
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 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 = 0;
  48.     ll val = 0;
  49.     using a3l = array<ll, 3>;
  50.     vector<a3l> post(n + 1);
  51.     post[n] = {0, n, n};
  52.     int op1 = n, op2 = n;
  53.     for (int i = n, j = n; i > 0; --i) {
  54.         val += arr[i - 1];
  55.         if (val >= maxv)
  56.             maxv = val, op1 = i - 1, op2 = j;
  57.         if (val <= 0)
  58.             j = i - 1, val = 0;
  59.         post[i - 1] = {maxv, op1, op2};
  60.     }
  61.     maxv = -1e18;
  62.     for (int i = 0; i <= n; ++i) {
  63.         val = prev[i][0] + post[i][0];
  64.         if (val > maxv)
  65.             dbg(i, prev[i][0], post[i][0]), dbg(post[i][0], post[i][1], post[i][2]), a = prev[i][1], b = post[i][1],
  66.                                                                                      c = post[i][2], maxv = val;
  67.     }
  68.     dbg(2 * maxv - acc[n]);
  69.     dbg(acc[a], acc[b], acc[c], acc[n]);
  70.     cout << a << ' ' << b << ' ' << c << endl;
  71.     return 0;
  72. };
  73.  
  74. int main1(int argc, char **argv)
  75. {
  76.     int n;
  77.     cin >> n;
  78.     vi arr(n);
  79.     for (auto &x : arr)
  80.         cin >> x;
  81.     vl acc(n + 1);
  82.     for (int i = 0; i < n; ++i)
  83.         acc[i + 1] = acc[i] + arr[i];
  84.     int a = 0, b = 0, c = 0;
  85.  
  86.     using a2l = array<ll, 2>;
  87.     vector<a2l> prev(n + 1);
  88.  
  89.     for (int i = 1; i <= n; ++i) {
  90.         if (acc[i] > prev[i - 1][0])
  91.             prev[i] = a2l{acc[i], i};
  92.         else
  93.             prev[i] = prev[i - 1];
  94.         dbg(prev[i][0], prev[i][1]);
  95.     }
  96.     ll maxv = -1e18;
  97.     ll val = 0;
  98.     dbg(val, prev[n][1], n);
  99.     ll suf = acc[n], k = n;
  100.     for (int i = n; i >= 0; --i) {
  101.         if (acc[i] > suf)
  102.             suf = acc[i], k = i;
  103.         ll t = prev[i][0] - acc[i] + suf;
  104.         if (t > maxv)
  105.             maxv = t, a = prev[i][1], b = i, c = k;
  106.     }
  107.  
  108.     cout << a << ' ' << b << ' ' << c << endl;
  109.     return 0;
  110. };
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement