#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);
cout << "i: " << i << " " << dp[i].fi << " " << -dp[i].se << "\n";
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
segments.cpp: In function 'int main()':
segments.cpp:58:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int c = l + r >> 1;
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |