This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 500000;
int n;
int a[MAXN+1];
int pre[MAXN+1];
pii dp[MAXN+1];
pii ran[MAXN+1];
int ans;
void solve(int id, int k) {
int l = ran[k].ff, r = ran[k].ss;
while (l < r) {
int mid = (l + r + 1) / 2;
if (pre[id] - pre[mid] >= dp[mid].ss)
l = mid;
else
r = mid - 1;
}
if (pre[id] - pre[l] >= dp[l].ss)
dp[id] = pii(dp[l].ff + 1, pre[id] - pre[l]);
return;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], pre[i] = pre[i-1] + a[i];
for (int i = 1; i <= n; i++)
ran[i] = pii(n+1, 0);
for (int i = 1; i <= n; i++) {
solve(i, dp[i-1].ff);
if (!dp[i].ff)
solve(i, dp[i-1].ff-1);
int nx = dp[i].ff;
ran[nx].ff = min(ran[nx].ff, i);
ran[nx].ss = max(ran[nx].ss, i);
}
cout << dp[n].ff << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |