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>
#define fi first
#define se second
#define y1 theboatman
using namespace std;
typedef long long ll;
const int N = int(1e6) + 10;
const int inf = int(1e9) + 10;
const long long INF = ll(1e18) + 10;
vector <ll> pref;
ll get(int l, int r) {
return pref[r] - (l ? pref[l - 1] : 0);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
vector <int> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
pref.resize(n);
for(int i = 0; i < n; i++) {
pref[i] = (i ? pref[i - 1] + a[i] : a[i]);
}
vector <pair <ll, int> > dp(n);
for(int i = 0; i < n; i++) {
dp[i] = {get(0, i), 1};
}
vector <pair <ll, int> > lazy(n, {INF, inf});
pair <ll, int> now = {0LL, -1};
for(int i = 0; i < n; i++) {
now.fi += a[i];
now = min(now, lazy[i]);
dp[i] = min(dp[i], now);
if (i + 1 == n || get(i + 1, n - 1) < now.fi) {
continue;
}
int l = i + 1, r = n - 1;
while(l < r) {
int c = l + r >> 1;
if (get(i + 1, c) < now.fi) {
l = c + 1;
}
else {
r = c;
}
}
lazy[l] = min(lazy[l], {get(i + 1, l), now.se - 1});
}
cout << -dp[n - 1].se << "\n";
return 0;
}
/*
*/
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:57:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int c = l + r >> 1;
~~^~~
# | 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... |