#include <bits/stdc++.h>
//#include "grader.h"
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N1 = 5e5 + 123;
int a[N1], b[N1], c[N1], n, m, k, z, w, ans, x, y, dp[N1];
vector <pii> v;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
int t[4 * N1];
int merge( int a, int b )
{
return min(a, b);
}
void update( int pos, int val, int v = 1, int tl = 1, int tr = n )
{
if( tl == tr )
{
t[v] = val;
return;
}
int tm = tl + tr >> 1;
if( pos <= tm ) update( pos, val, v + v, tl, tm );
else update( pos, val, v + v + 1, tm + 1, tr );
t[v] = merge( t[v + v], t[v + v + 1] );
}
int get( int x, int v = 1, int tl = 1, int tr = n )
{
if( tl == tr ) return tl;
int tm = tl + tr >> 1;
if( t[v + v + 1] <= x ) return get( x, v + v + 1, tm + 1, tr );
else return get( x, v + v, tl, tm );
}
void solve()
{
cin >> n;
FOR( i, 0, 4 * n, 1 ) t[i] = 1e18;
FOR( i, 1, n, 1 ) cin >> a[i], b[i] = b[i - 1] + a[i];
FOR( i, 1, n, 1 )
{
// for i find max j (cj + bj <= bi)
int j = 0;
if( t[1] <= b[i] ) j = get(b[i]);
dp[i] = dp[j] + 1;
c[i] = b[i] - b[j];
update( i, c[i] + b[i] );
}
cout << dp[n] << '\n';
}
signed main()
{
// freopen("connect.in", "r", stdin);
// freopen("connect.out", "w", stdout);
respagold
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
# | 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... |