#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define lid chs[id].first
#define rid chs[id].second
#define mid ((r + l) >> 1)
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
const int MAX_N = 5e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const ld EPS = 1e-9;
const int LOG = 60;
vector<pair<int, int>> chs;
vector<short> seg;
int ind = 1;
void node(int l, int r, int id)
{
if (chs[id].first || l == r - 1) return;
chs[id].first = ind++;
chs[id].second = ind++;
chs.push_back({0, 0}); chs.push_back({0, 0});
seg.push_back(0); seg.push_back(0);
}
void upd(ll x, short y, ll l, ll r, int id)
{
node(l, r, id);
if (l == r - 1)
{
seg[id] = max(seg[id], y);
return;
}
if (x < mid) upd(x, y, l, mid, lid);
else upd(x, y, mid, r, rid);
seg[id] = max(seg[lid], seg[rid]);
}
int get(ll s, ll t, ll l, ll r, int id)
{
node(l, r, id);
if (s <= l && t >= r) return seg[id];
if (s >= r || t <= l) return 0;
return max(get(s, t, l, mid, lid), get(s, t, mid, r, rid));
}
void solve() {
int n;
cin >> n;
int a[n];
ll ps[n + 1];
ps[0] = 0;
for (int i = 0; i < n; i++) cin >> a[i], ps[i + 1] = ps[i] + a[i];
pair<int, ll> dp[n + 1];
dp[0] = {0, 0};
chs.push_back({0, 0});
seg.push_back(0);
upd(0, 0, 0, INF, 0);
for (int i = 0; i < n; i++)
{
int ind = get(0, ps[i + 1] + 1, 0, INF, 0);
dp[i + 1] = {dp[ind].first + 1, ps[i + 1] - ps[ind]};
upd(dp[i + 1].second + ps[i + 1], i + 1, 0, INF, 0);
}
cout << dp[n].first << "\n";
}
int main() {
sui;
int tc = 1;
//cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
| # | 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... |