#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define int long long int
const int N = 3e5 + 10;
const int md = 1e9 + 7;
const int INF = 1e9;
int32_t main(int32_t argc, char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vector<int> pref(n + 1);
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
vector<pair<int, int>> dp(n + 1);
dp[1] = {a[1], 1};
for (int i = 2; i <= n; i++) {
dp[i] = {dp[i - 1].first + a[i], dp[i - 1].second};
int l = 1, r = i + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (pref[i] - pref[mid - 1] >= dp[mid - 1].first)
l = mid;
else r = mid;
}
if (pref[i] - pref[l - 1] >= dp[l - 1].first) {
dp[i].first = pref[i] - pref[l - 1];
dp[i].second = dp[l - 1].second + 1;
}
}
cout << dp[n].second << '\n';
}
return 0;
}
# | 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... |