/**
____ ____ ____ __________________ ____ ____ ____
||I || ||c || ||e || || || ||M || ||a || ||n ||
||__|| ||__|| ||__|| ||________________|| ||__|| ||__|| ||__||
|/__\| |/__\| |/__\| |/________________\| |/__\| |/__\| |/__\|
*/
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
#define maxn 500005
#define maxlog 20
#define INF 1000000010
#define LINF 1000000000000000005
#define endl '\n'
#define pb(x) push_back(x)
#define X first
#define Y second
#define control cout<<"passed"<<endl;
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
typedef pair <ll, ll> pll;
typedef pair <int, ll> pil;
typedef pair <ll, int> pli;
typedef long double ld;
ll tree[maxn * 4];
void build(int node , int l , int r)
{
if(l == r)
{
tree[node] = 1e18;
return;
}
int mid = (l + r) / 2;
build(node * 2 , l , mid);
build(node * 2 + 1 , mid + 1 , r);
tree[node] = min(tree[node * 2] , tree[node * 2 + 1]);
}
void update(int node , int l , int r , int qpos , ll qval)
{
if(l == r)
{
tree[node] = qval;
return;
}
int mid = (l + r) / 2;
if(qpos <= mid)
update(node * 2 , l , mid , qpos , qval);
else
update(node * 2 + 1 , mid + 1 , r , qpos , qval);
tree[node] = min(tree[node * 2] , tree[node * 2 + 1]);
}
ll query(int node , int l , int r , int ql , int qr)
{
if(l > qr || r < ql)
return 1e18;
if(l >= ql && r <= qr)
return tree[node];
int mid = (l + r) / 2;
return min(query(node * 2 , l , mid , ql , qr) ,
query(node * 2 + 1 , mid + 1 , r , ql , qr));
}
int n;
int a[maxn];
pll dp[maxn];
ll pref[maxn];
ll minn[maxn];
void read()
{
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
dp[1] = {1, a[1]};
build(1 , 1 , n);
update(1 , 1 , n , 1 , a[1] + a[1]);
pref[0] = 0;
for(int i = 1; i <= n; i++)
pref[i] = pref[i - 1] + a[i];
for(int i = 2; i <= n; i++)
{
int l = 1, r = i - 1;
int pos = -1;
while(l <= r)
{
int mid = (l + r) / 2;
if(query(1 , 1 , n , 1 , mid) <= pref[i])
l = mid + 1, pos = mid;
else
r = mid - 1;
}
if(pos == -1)
dp[i] = {dp[i - 1].X, dp[i - 1].Y + a[i]};
else
dp[i] = {dp[pos].X + 1, pref[i] - pref[pos]};
update(1 , 1 , n , i , dp[i].Y + pref[i]);
}
//for(int i = 1; i <= n; i++)
// cout << dp[i].X << " " << dp[i].Y << endl;
cout << dp[n].X << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read();
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... |