Submission #1337990

#TimeUsernameProblemLanguageResultExecution timeMemory
1337990JuanJLRastuci (COCI25_rastuci)C++20
110 / 110
639 ms196364 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define fst first
#define snd second
#define pb push_back
#define mset(a,v) memset(a,v,sizeof(a))
#define forn(i,a,b) for(int i = a; i<b; i++)
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

template <typename T>
using iset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#ifdef D
	#define debug(x) cout<<x
#else
	#define debug(x) //nothing
#endif

const int MAXN = 5003;

int n;
ll ps[MAXN];

pair<int,int> dmax[MAXN][MAXN];

int main(){ FIN;
	cin>>n;
	
	forn(i,1,n+1) cin>>ps[i];
	
	ps[0]=0;
	forn(i,2,n+1) ps[i]+=ps[i-1];


	forn(i,0,n+2) forn(j,0,n+2) dmax[i][j]={-10000000,-1};

	forn(i,1,n+1) dmax[i][n]={1,n};
	int l,r,mid;
	for(int i = n; i>=1; i--){
		for(int j = n-1; j>=i; j--){
			 l = 1;  r=n;
			while(l<=r){
				mid = (l+r)/2;
				if(ps[mid]-ps[j]>=ps[j]-ps[i-1]) r=mid-1;
				else l = mid+1;
			}
			if(l<=n) dmax[i][j]={dmax[j+1][l].fst+1,j};
		}
		for(int j = n-1; j>=0; j--){
			dmax[i][j]=max(dmax[i][j],dmax[i][j+1]);
		}
	}
	
	
	cout<<dmax[1][1].fst<<'\n';

	int L = 1;
	int R = dmax[1][1].snd;

	cout<<ps[R]-ps[L-1]<<' ';

	while(R!=n){
		int nL = R+1;
		int nR;

		 l = 1;  r=n;
		while(l<=r){
			mid = (l+r)/2;

			if(ps[mid]-ps[nL-1]>=ps[R]-ps[L-1]) r=mid-1;
			else l = mid+1;
		}
		nR=dmax[nL][l].snd;
		cout<<ps[nR]-ps[nL-1]<<' ';
		L=nL;
		R=nR;
	}
	cout<<'\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...
#Verdict Execution timeMemoryGrader output
Fetching results...