제출 #384376

#제출 시각아이디문제언어결과실행 시간메모리
384376vanicBigger segments (IZhO19_segments)C++14
0 / 100
1 ms364 KiB
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <array>

using namespace std;

typedef long long ll;
const int maxn=5e5+5;


ll a[maxn];
ll pref[maxn];
array < ll, 3 > dp[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]={a[0], -1, 1};
	int pos=-1;
	for(int i=1; i<n; i++){
		while(pos<i-1 && pref[i+1]-pref[pos+2]>=dp[pos+1][0]){
			pos++;
		}
//		cout << pref[i+1]-pref[pos+1] << endl;
		if(pref[i+1]-pref[pos+1]>=dp[pos][0]){
			dp[i]={pref[i+1]-pref[pos+1], pos, dp[i-1][2]+1};
			pos=i;
			continue;
		}
		dp[i]={pref[i+1]-pref[pos+1], pos, dp[i-1][2]};
	}
	cout << dp[n-1][2] << '\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...