/*
subtask 2 -> dp[i] = max(dp[i], dp[j]+1) -> dengan syarat dari j+1 hingga
i > total segment sebelumnya
maintain dp[j] max brp segment, total segment sekecil mungkin berapa
subtask 3 -> optimasi
subtask 5 -> O(N)
ubah dp menjadi dp push
dp[j] > dp[j-1]
cari nilai terdekat di depan yang segmentnya lebih besar
dari segmen sekarang (binser)
*/
#include <bits/stdc++.h>
#define ll long long
#define KAGAMINE ios_base::sync_with_stdio(false);
#define LEN cin.tie(NULL);
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
#define vector2d(x) vector<vector<x>>
#define pii pair<int, int>
#define pl pair<ll, ll>
using namespace std;
int main(){
KAGAMINE LEN
ll n; cin >> n;
vector<ll> ar(n+5);
vector<ll> dp(n+5, 0), pos(n+5, 0);
vector<ll> pf(n+5);
for (ll i = 1; i <= n; i++){
cin >> ar[i];
}
pf[0] = 0;
pf[n+1] = 1e18;
for (ll i = 1; i <= n; i++){
pf[i] = pf[i-1]+ar[i];
}
for (ll i = 1; i <= n; i++){
pos[i] = max(pos[i],pos[i-1]);
dp[i] = dp[pos[i]]+1;
ll lb = lower_bound(pf.begin()+1, pf.end(), pf[i]*2 - pf[pos[i]]) - pf.begin();
pos[lb] = i;
}
cout << dp[n];
return 0;
}
# | 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... |