Submission #525764

#TimeUsernameProblemLanguageResultExecution timeMemory
525764Alex_tz307Bigger segments (IZhO19_segments)C++17
100 / 100
75 ms12100 KiB
#include <bits/stdc++.h>

using namespace std;

void minSelf(int64_t &x, int64_t y) {
  if (y < x) {
    x = y;
  }
}

void maxSelf(int &x, int y) {
  if (x < y) {
    x = y;
  }
}

void testCase() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<int64_t> pref(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  vector<int64_t> minSum = pref;
  vector<int> maxSeg(n + 1, 1);
  for (int i = 1; i <= n; ++i) {
    minSelf(minSum[i], minSum[i - 1] + a[i]);
    maxSelf(maxSeg[i], maxSeg[i - 1]);
    int j = distance(pref.begin(), lower_bound(pref.begin() + 1, pref.end(), minSum[i] + pref[i]));
    if (j <= n) {
      maxSeg[j] = maxSeg[i] + 1;
      minSum[j] = pref[j] - pref[i];
    }
  }
  cout << maxSeg[n] << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  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...