이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[0][i] = 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 = 0; k <= j; k++) {
int sum2 = pre[i] - pre[j];
int sum1 = pre[j] - pre[k];
if (sum1 <= sum2) {
dp[j][i] = max(dp[j][i], dp[k][j] + 1);
}
}
}
}
int res = 0;
for (int i = 0; i <= n; i++) res = max(res, dp[i][n]);
cout << res;
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In member function 'void IT::adj(int, int, int, int, int)':
segments.cpp:47:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
segments.cpp: In member function 'int IT::get(int, int, int, int, int)':
segments.cpp:58:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | 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... |