Submission #990912

#TimeUsernameProblemLanguageResultExecution timeMemory
990912ToniBBigger segments (IZhO19_segments)C++17
100 / 100
300 ms50260 KiB
#include <bits/stdc++.h>
using namespace std;

#define X first
#define Y second

typedef long long ll;
typedef pair<int, ll> pii;

const int N = 5e5 + 2;

int n, a[N];
pii dp[N];
ll pre[N];
set<pair<ll, pii> > s;

pii cmp_dp(pii x, pii y) {
	if (x.X == y.X) {
		if (x.Y < y.Y) return x;
		return y;
	}
	if (x.X > y.X) return x;
	return y;
}
bool higher(pii x, pii y) {
	if (x.X == y.X) return x.Y > y.Y;
	return x.X > y.X;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n;
	for (int i = 0; i < n; ++i) cin >> a[i];

	pre[0] = a[0];
	for (int i = 1; i < n; ++i) pre[i] = a[i] + pre[i - 1];

	for (int i = 0; i < n; ++i) {
		dp[i] = {1, pre[i]};
		
		auto it = s.lower_bound({pre[i] + 1, {0, 0}});
		
		if (it != s.begin()) {
			--it;
			dp[i] = cmp_dp(dp[i], {(*it).Y.X + 1, pre[i] - (*it).Y.Y});
		}
		
		pii cur = {dp[i].X, pre[i]};
		ll x = dp[i].Y + pre[i];
		
		it = s.lower_bound({x, {0, 0}});
		
		while (it != s.end() && !higher((*it).second, cur)) {
			s.erase(it);
			it = s.lower_bound({x, {0, 0}});
		}
		
		it = s.lower_bound({x + 1, {0, 0}});

		if (it != s.begin() && !higher(cur, (*--it).second)) continue;
		s.insert({x, cur});
	}

	cout << dp[n - 1].X << "\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...