#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<long long> pref(N + 1);
for(int i = 1; i <= N; ++i){
cin >> pref[i];
pref[i] += pref[i - 1];
}
vector<int> dp(N + 1), pos(N + 1);
for(int i = 1; i <= N; ++i){
pos[i] = max(pos[i], pos[i - 1]);
dp[i] = dp[pos[i]] + 1;
//(pos[i], i] <= (i, x] with x minimum
//=> pref[i] - pref[pos[i]] <= pref[x] - pref[i]
//=> 2 * pref[i] - pref[pos[i]]; <= pref[x]
int ub = lower_bound(pref.begin(), pref.end(), 2 * pref[i] - pref[pos[i]]) - pref.begin();
if(ub < (int)pref.size()) pos[ub] = max(pos[ub], i);
}
cout << dp[N] << '\n';
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... |