Submission #990891

#TimeUsernameProblemLanguageResultExecution timeMemory
990891ToniBBigger segments (IZhO19_segments)C++17
37 / 100
151 ms71256 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 3005;
typedef long long ll;

int n, a[N], dp[N][N], opt[N][N];
ll pre[N];

ll sum(int i, int j) {
	if (i) return pre[j] - pre[i - 1];
	return pre[j];
}

int main(){
	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) {
		for (int j = 0; j < n; ++j) dp[i][j] = opt[i][j] = -1e9;
	}
	
	for (int i = 0; i < n; ++i) {
		dp[0][i] = opt[0][i] = 1;
	}
	
	for (int j = 1; j < n; ++j) {
		for (int i = 1; i <= j; ++i) {
			int lo = 0, hi = i;
			while (lo < hi) {
				int mid = (lo + hi) >> 1;
				if (sum(i, j) >= sum(mid, i - 1)) hi = mid;
				else lo = mid + 1;
			}
			dp[i][j] = max(dp[i][j], opt[lo][i - 1] + 1);
			opt[i][j] = dp[i][j];
		}
		for (int i = n - 2; i >= 0; --i) {
			opt[i][j] = max(opt[i][j], opt[i + 1][j]);
		}
	}
	
	int ans = 0;
	for (int i = 0; i < n; ++i) ans = max(ans, dp[i][n - 1]);

	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...