Submission #155682

#TimeUsernameProblemLanguageResultExecution timeMemory
155682qkxwsmBigger segments (IZhO19_segments)C++14
100 / 100
173 ms28852 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 500013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
ll arr[MAXN], pref[MAXN];
pll dp[MAXN];
pll fen[MAXN];
int ans;

void update(int idx, pll p)
{
	for (int e = idx + 1; e <= N + 2; e += e & (-e))
	{
		ckmax(fen[e], p);
	}
	return;
}
pll query(int idx)
{
	pll res;
	for (int e = idx + 1; e; e -= e & (-e))
	{
		ckmax(res, fen[e]);
	}
	return res;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	FOR(i, 0, N)
	{
		cin >> arr[i];
		pref[i + 1] = pref[i] + arr[i];
	}
	dp[0] = {0, 0}; //best, sum needed
	FOR(i, 0, N)
	{
		//you need pref to be >= dp[j].se
		int idx = LB(pref, pref + N + 1, pref[i] + dp[i].se) - pref;
		update(idx, {dp[i].fi + 1, pref[i]});
		pll p = query(i + 1);
		dp[i + 1] = {p.fi, pref[i + 1] - p.se};
	}
	// FOR(i, 1, N + 1)
	// {
	// 	FORD(j, i, 0)
	// 	{
	// 		if (pref[i] >= dp[j].se + pref[j])
	// 		{
	// 			dp[i].fi = dp[j].fi + 1;
	// 			dp[i].se = pref[i] - pref[j];
	// 			break;
	// 		}
	// 	}
	// }
	//ok cool so we only need an update suffix, query point
	ans = dp[N].fi;
	//choose the rightmost umber to go off of
	cout << ans << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...