This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n;
ll a[N], dp[N][2], pref[N];
vector<int> dps[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
dp[0][0] = 0;
dp[0][1] = 0;
pref[0] = 0;
for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + a[i], dps[i].pb(0);
for(int i = 1; i <= n; ++i){
dp[i][0] = 0;
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1] + a[i];
for(int j: dps[i]){
ll s = pref[i] - pref[j];
if(s >= dp[j][1]){
if(dp[i][0] < dp[j][0] + 1){
dp[i][0] = dp[j][0] + 1;
dp[i][1] = s;
}else if(dp[i][0] == dp[j][0] + 1) dp[i][1] = min(dp[i][1], s);
}
}
int l = i + 1, r = n, cc = n + 1;
while(l <= r){
int m = l+r>>1;
if(pref[m] - pref[i] >= dp[i][1]){
cc = m;
r = m - 1;
}else l = m + 1;
}
dps[cc].pb(i);
}
cout << dp[n][0];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
segments.cpp: In function 'void solve()':
segments.cpp:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int m = l+r>>1;
| ~^~
segments.cpp: In function 'int main()':
segments.cpp:52:15: warning: unused variable 'aa' [-Wunused-variable]
52 | int tt = 1, aa;
| ^~
# | 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... |