//gm --- akezhon
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NE r < tl || tr < l
#define int long long
using namespace std;
const int N=5e5+7;
const int mod=998244353;
const int inf=2e18;
int dp[N];
int sm[N];
int a[N];
int p[N];
int n;
int t[20][N];
int L[N];
void AlemAmenov(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
p[i] = p[i-1] + a[i];
sm[i]=inf;
}
for(int i=1; i <= n; i++){
dp[i] = dp[i-1];
sm[i] = sm[i-1]+a[i];
int l = L[dp[i]], r=i-1;
for(int j=19; j >= 0; j--){
if(l <= r-(1<<j)+1 && t[j][r] > p[i])r-=(1<<j);
}
if(t[0][r] <= p[i]){
dp[i] = dp[r]+1;
sm[i] = p[i]-p[r];
}
else if(dp[i] > 0){
l = L[dp[i]-1], r=L[dp[i]]-1;
for(int j=19; j >= 0; j--){
if(l <= r-(1<<j)+1 && t[j][r] > p[i])r-=(1<<j);
}
if(t[0][r] <= p[i] && p[i]-p[r] < sm[i]){
sm[i] = p[i]-p[r];
}
}
if(L[dp[i]] == 0)L[dp[i]]=i;
t[0][i] = sm[i]+p[i];
for(int j=1; j < 20; j++){
if(1 <= i-(1<<j)+1)t[j][i] = min(t[j-1][i], t[j-1][i-(1<<(j-1))]);
}
}
cout << dp[n];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int RealName=1;
// cin >> RealName;
// srand(time(0));
while(RealName--)
AlemAmenov();
return 0;
}
# | 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... |