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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define vt vector
#define pb push_back
#define pii pair<int, int>
#define sz(dq) (int)dq.size()
#define forr(i, a, b) for(int i = a; i < b; i++)
#define fi first
#define se second
#define pll pair<ll, ll>
ifstream fin("store.inp");
ofstream fout("store.out");
const ll mxn = 5e5 + 5, inf = 1e9, mod = 1e9 + 7, sq = 800, mxv = 1e6 + 5, pr = 37, mod2 = 1e9 + 9, mod3 = 998244353;
//const int x[4] = {0, -1, 0, 1};
int n;
pll dp[mxn + 1];
ll a[mxn + 1], pref[mxn + 1];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i]; pref[i] = pref[i - 1] + a[i];
}
dp[0] = make_pair(0, 0);
for(int i = 1; i <= n; i++){
int l = 0, r = i - 1, ans;
while(l <= r){
int mid = (l + r) >> 1;
if(dp[mid].second <= pref[i] - pref[mid]){
ans = mid; l = mid + 1;
}else{
r = mid - 1;
}
}
dp[i] = make_pair(dp[ans].first + 1, pref[i] - pref[ans]);
}
cout << dp[n].first;
return(0);
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:39:35: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
39 | dp[i] = make_pair(dp[ans].first + 1, pref[i] - pref[ans]);
| ~~~~~~~~^~~~~
# | 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... |