//*** Bismillah ***//
#pragma GCC optimize("O3", "fast-math", "unroll-loops", "no-stack-protector")
#include <bits/stdc++.h>
using namespace std;
#if !defined(ONLINE_JUDGE) and !defined(EVAL)
#include "template/debug.h"
#else
#define d(x...)
#endif
#define fr first
#define er erase
#define sc second
#define in insert
#define ll long long
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define ull unsigned ll
#define vpll vector<pll>
#define len(x) (ll) x.size()
#define all(x) x.begin(),x.end()
const ll INF = 1e9;
const ll INFL = 1e18;
const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll maxn = 2e5+5;
ll n,m,k=0;
ll a[maxn], ps[maxn], dp[maxn], mn[maxn];
void _(ll &tt){
cin >> n;
for(ll i = 1; i <= n; i ++){
cin >> a[i];
mn[i] = INFL;
ps[i] = ps[i - 1] + a[i];
}
mn[0] = 0;
for(ll i = 1; i <= n; i ++){
// for(ll j = i - 1; j >= 0; j --){
// if(mn[j] <= ps[i] - ps[j]){
// if(dp[i] < dp[j] + 1){
// dp[i] = dp[j] + 1;
// mn[i] = ps[i] - ps[j];
// }
// else if(dp[i] == dp[j] + 1){
// mn[i] = min(mn[i], ps[i] - ps[j]);
// }
// break;
// }
// }
// bs for last j that mn[j] <= ps[i] - ps[j]
ll l = 1, r = n, j;
while(l <= r){
ll md = (l + r) >> 1;
if(mn[md] <= ps[i] - ps[md]) l = md + 1, j = md;
else r = md - 1;
}
if(mn[j] <= ps[i] - ps[j]){
if(dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
mn[i] = ps[i] - ps[j];
}
else if(dp[i] == dp[j] + 1){
mn[i] = min(mn[i], ps[i] - ps[j]);
}
}
}
// for(ll i = 1; i <= n; i ++) cout << dp[i] << ' '; cout << '\n';
// for(ll i = 1; i <= n; i ++) cout << mn[i] << ' '; cout << '\n';
cout << dp[n] << '\n';
}
signed main() {
ll tm = clock();
cin.tie(0)->sync_with_stdio(0);
ll t = 1;
// cin >> t;
for(ll tt = 1; tt <= t; tt ++) {
_(tt);
}
cerr << "\n\033[1;31mTime: \033[1;30m" \
<< (double)(clock()-tm)/1000000 << "\033[1;32m seconds\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... |