# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1133827 | lopkus | Bigger segments (IZhO19_segments) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
/*
for(int i = 1; i <= n; i++) {
int sum = 0;
int mx = 0;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= e[j - 1]) {
mx = max(mx, dp[j - 1]);
}
}
sum = 0;
int idx = - 1;
int mn = 1e18;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= e[j - 1] && mx == dp[j - 1]) {
if(mn > sum) {
idx = j - 1;
mn = min(mn, sum);
}
}
}
if(idx == - 1) {
dp[i] = dp[i - 1];
e[i] = e[i - 1] + a[i];
}
else {
dp[i] = dp[idx] + 1;
e[i] = mn;
}
}*/
vector<int> dp(n + 1, 0);
vector<int> e(n + 1, 0);
e[0] = 0;
dp[0] = 0;
vector<int> pref(n + 1, 0);
for(int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
for(int i = 1; i <= n; i++) {
int p = - 1;
for(int j = i; j <= n; j++) {
if(pref[j] - pref[i - 1] >= e[i - 1]) {
p = j;
break;
}
}
if(i == 1) {
dp[i] = 1;
e[i] = a[1];
}
if(a[i] >= e[i - 1]) {
if(dp[i] < dp[i - 1] + 1) {
dp[i] = dp[i - 1] + 1;
e[i] = a[i];
}
}
if(p == - 1) {
continue;
}
for(int j = p; j <= n; j++) {
if(dp[j] < dp[i - 1] + 1) {
dp[j] = dp[i - 1] + 1;
e[j] = pref[j] - pref[i - 1];
}
if(dp[j] == dp[i - 1] + 1) {
e[j] = min(e[j], pref[j] - pref[i - 1]);
}
}
}
cout << dp[n];
}