#include <bits/stdc++.h>
/*
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target ("avx2")
*/
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define F first
#define S second
#define pb push_back
#define FIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define md(a) ((a%mod+mod)%mod)
#define all(a) a.begin(), a.end()
#define MP make_pair
#define lc (id<<1)
#define rc (lc|1)
#define mid (l+r)/2
#define SZ(a) (ll)(a.size())
#define kill(a) cout << a << "\n", exit(0)
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef long long ll;
typedef long double ld;
typedef vector<vector<ll>> matrix;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn=1e6+10, mod=998244353, INF=1e14+10, LOG=21, sq=200;
ll poww(ll a, ll b, ll mod) {
if (b == 0) return 1;
return 1 * poww(1 * a * a % mod, b / 2, mod) * ((b % 2 == 1) ? a : 1) % mod;
}
ll n, ps[maxn], seg[maxn<<2], dp[maxn];
void Set(ll p, ll x, ll l=1, ll r=n+1, ll id=1)
{
if(l==r-1){
seg[id]=x;
return;
}
if(p<mid) Set(p, x, l, mid, lc);
else Set(p, x, mid, r, rc);
seg[id]=min(seg[lc], seg[rc]);
}
ll Get(ll x, ll l=1, ll r=n+1, ll id=1)
{
if(l==r-1) return l;
if(seg[rc]<=x) return Get(x, mid, r, rc);
else return Get(x, l, mid, lc);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(ll i=1;i<=n;i++)
{
ll a;
cin>>a;
ps[i]=ps[i-1]+a;
}
fill(seg, seg+maxn*4, INF);
for(ll i=1;i<=n;i++)
{
ll k=0;
if(seg[1]>ps[i]) k=0;
else k=Get(ps[i]);
dp[i]=dp[k]+1;
Set(i, 2*ps[i]-ps[k]);
// cout<<i<<" "<<k<<" "<<dp[k]<<" "<<dp[i]<<"\n";
}
cout<<dp[n]<<"\n";
}
# | 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... |