Submission #384330

#TimeUsernameProblemLanguageResultExecution timeMemory
384330vanicBigger segments (IZhO19_segments)C++14
13 / 100
2 ms2412 KiB
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;
const int maxn=3005;

ll a[maxn];
ll pref[maxn];
ll dp[maxn][maxn];
int gran[maxn];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> a[i];
		pref[i+1]=pref[i]+a[i];
	}
	dp[0][1]=a[0];
	for(int i=1; i<n; i++){
		for(int j=1; j<=n; j++){
			if(!dp[i-1][j]){
				while(pref[i+1]-pref[gran[j-1]+2]>=dp[gran[j-1]+1][j-1]){
					gran[j-1]++;
				}
				if(dp[gran[j-1]][j-1]<=pref[i+1]-pref[gran[j-1]+1]){
					dp[i][j]=pref[i+1]-pref[gran[j-1]+1];
//					cout << "gradim " << j << ' ' << i << endl;
					gran[j]=i;
				}
				break;
			}
			else if(j>1){
				while(pref[i+1]-pref[gran[j-1]+2]>=dp[gran[j-1]+1][j-1]){
					gran[j-1]++;
				}
				dp[i][j]=pref[i+1]-pref[gran[j-1]+1];
				if(dp[gran[j]][j]-pref[i+1]+pref[gran[j]+1]>dp[i][j]){
					gran[j]=i;
				}
			}
			else{
				dp[i][j]=pref[i+1];
			}
		}
	}
	int br;
	for(int i=1; i<=n; i++){
		if(dp[n-1][i]){
			br=i;
		}
	}
	cout << br << '\n';
	return 0;
}

Compilation message (stderr)

segments.cpp: In function 'int main()':
segments.cpp:60:16: warning: 'br' may be used uninitialized in this function [-Wmaybe-uninitialized]
   60 |  cout << br << '\n';
      |                ^~~~
#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...