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;
#define Task ""
struct __Init__ {
__Init__() {
cin.tie(nullptr)->sync_with_stdio(false);
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout); }
}
} __init__;
using ll = long long;
#ifdef LOCAL
#define debug(x) cerr << "[" #x " = " << x << "]\n";
#else
#define debug(...)
#endif // LOCAL
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
template<class C> int isz(const C& c) { return c.size(); }
template<class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
constexpr int eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;
// =============================================================================
constexpr int maxN = 5e5 + 5;
int n;
int a[maxN];
ll pref[maxN];
int dp[maxN];
vector<pair<ll, int>> save[maxN];
vector<int> best[maxN];
signed main() {
cin >> n;
For (i, 1, n) cin >> a[i];
For (i, 1, n) {
pref[i] = pref[i - 1] + a[i];
}
save[0].emplace_back(0, 0);
best[0].push_back(0);
For (i, 1, n) {
Ford (j, i, 1) {
// a[j..i] + dp[j - 1]
ll sum = pref[i] - pref[j - 1];
auto it = lower_bound(all(save[j - 1]), make_pair(sum, inf));
if (it == save[j - 1].begin()) continue;
--it;
int pre = best[j - 1][it - save[j - 1].begin()];
chmax(dp[i], pre + 1);
save[i].emplace_back(sum, pre + 1);
}
sort(all(save[i]));
best[i].resize(isz(save[i]));
best[i][0] = save[i][0].se;
For (j, 1, isz(best[i]) - 1) {
best[i][j] = max(best[i][j - 1], save[i][j - 1].se);
}
}
cout << *max_element(dp + 1, dp + n + 1) << '\n';
}
/*
*/
Compilation message (stderr)
segments.cpp: In constructor '__Init__::__Init__()':
segments.cpp:11:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:12:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen(Task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |