# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
708148 | arcane | Bigger segments (IZhO19_segments) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
#define int long long
#define mp make_pair
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, ans = 0, tmp1 = -1e8, tot, pos; cin >> n;
int arr[n], smol[n], pref[n + 1];
for (int i = 0; i < n; i++) cin >> arr[i];
pref[0] = 0;
set <pair <int, int> > s;
for (int i = 0; i < n; i++) pref[i + 1] = pref[i] + arr[i];
for (int i = 0; i < n; i++){
smol[i] = pref[i + 1];
auto it = s.upper_bound(mp(pref[i + 1], (int)1e18));
if (it != s.begin()){
it--;
smol[i] = min(smol[i], pref[i + 1] + (*it).se);
}
s.insert(mp(smol[i] + pref[i + 1], -pref[i + 1]));
while ((*(s.rbegin())).se != -pref[i + 1]) s.erase(--s.end());
}
tot = pref[n] - smol[n - 1]; pos = n - 1; ans = 1;
while (tot){
while (pref[pos + 1] != tot){
pos--;
}
tot -= smol[pos]; ans += 1;
}
cout << ans << '\n';
}