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;
typedef vector<int> vi;
typedef long long ll;
const int N = 1e6 + 1;
int n, res;
int a[N];
void iuq(int i, ll lst_sum , ll sum, int cnt) {
if (i == n + 1) return void (res = max(res, cnt));
if (i < n) iuq(i + 1, lst_sum, sum + a[i], cnt);
if (lst_sum <= sum) {
iuq(i + 1, sum, a[i], cnt + 1);
}
}
void subtask1() {
iuq(1, 0, 0, 0);
cout << res;
}
void subtask2() {
vector<vi> dp(n + 1, vi(n + 1, -1e9));
for (int i = 1; i <= n; i++) dp[i][0] = 1;
vector<ll> pre(n + 1, 0);
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k < j; k++) {
int sum2 = pre[i] - pre[j - 1];
int sum1 = pre[j - 1] - pre[k - 1];
if (sum1 <= sum2) {
dp[i][j - 1] = max(dp[i][j - 1], dp[j - 1][k - 1] + 1);
}
}
}
}
cout << *max_element(dp[n].begin(), dp[n].end());
}
struct IT {
int n;
vi st;
IT(const int &_n = 0): n(_n), st(4 * _n + 1, -1e9) {}
void adj(int id, int l, int r, int i, int val) {
if (r < i || i < l) return ;
if (l == r) return void (st[id] = val);
int mid = l + r >> 1;
adj(id << 1, l, mid, i, val);
adj(id << 1 | 1, mid + 1, r, i, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void adj(int i, int val) {
adj(1, 0, n, i, val);
}
int get(int id, int l, int r, int u, int v) {
if (r < u || v < l) return -1e9;
if (u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int u, int v) {
return get(1, 0, n, u, v);
}
};
void subtask3() {
vector<vi> dp(n + 1, vi(n + 1, -1e9));
vector<IT> it;
it.resize(n + 1);
for (int i = 0; i <= n; i++) it[i] = IT(n);
dp[0][0] = 0;
it[0].adj(0, 0);
vector<ll> pre(n + 1, 0);
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
for (int i = 1; i <= n; i++) {
int k = 1;
for (int j = 1; j <= i; j++) {
int sum = pre[i] - pre[j - 1];
while (pre[j - 1] - pre[k - 1] > sum) k++;
if (k <= j) {
int cmax = it[j - 1].get(k - 1, j - 1);
dp[i][j - 1] = cmax + 1;
it[i].adj(j - 1, cmax + 1);
}
}
}
cout << *max_element(dp[n].begin(), dp[n].end());
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (n <= 20) {
subtask1();
return 0;
}
if (n <= 500) {
subtask2();
return 0;
}
subtask3();
return 0;
}
Compilation message (stderr)
segments.cpp: In member function 'void IT::adj(int, int, int, int, int)':
segments.cpp:45:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = l + r >> 1;
| ~~^~~
segments.cpp: In member function 'int IT::get(int, int, int, int, int)':
segments.cpp:56:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = 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... |