Submission #692530

#TimeUsernameProblemLanguageResultExecution timeMemory
692530taherBigger segments (IZhO19_segments)C++17
37 / 100
290 ms262144 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  } 
  vector<long long> pref(a.begin(), a.end());
  for (int i = 1; i < n; i++) {
    pref[i] += pref[i - 1];
  }
  const int inf = 123456789;
  vector<vector<int>> dp(n, vector<int> (n, -1));
  function<int(int, int)> DP = [&](int i, int j) {
    if (i == n) {
      return 1;
    }
    if (dp[i][j] != -1) {
      return dp[i][j];
    }
    int res = -inf;
    res = max(res, DP(i + 1, j));
    long long tSum = pref[i] - (j > 0 ? pref[j - 1] : 0);
    int low = j + 1, high = n - 1;
    while (low <= high) {
      int mid = low + (high - low) / 2;
      long long cSum = pref[mid] - pref[i];
      if (cSum >= tSum) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    if (high + 1 < n) {
      res = max(res, DP(high + 1, i + 1) + 1);
    }
    return dp[i][j] = res;
  };
  cout << DP(0, 0) << '\n';
  return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...