Submission #430018

#TimeUsernameProblemLanguageResultExecution timeMemory
430018mosiashvililukaThe short shank; Redemption (BOI21_prison)C++14
10 / 100
1329 ms108212 KiB
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,k,T,f[500009],F[500009],fen[500009],pas,msh[100009][19],msh2[100009][19];
vector <long long> dp[500009];
map <long long, long long> m;
map <long long, long long>::iterator it;
pair <long long, long long> P;
void upd(long long q, long long w){
	while(q<=a+2){
		if(fen[q]>w) fen[q]=w;
		q=q+(q&(-q));
	}
}
long long read(long long q){
	long long sm=a+3;
	while(q>=1){
		if(sm>fen[q]) sm=fen[q];
		q=q-(q&(-q));
	}
	return sm;
}
pair <long long, long long> cost(long long q, long long w){
	long long qw=0,we=0,h=0;
	for(h=17; h>=0; h--){
		if(msh[q][h]>w||msh[q][h]==0) continue;
		qw+=msh2[q][h];
		q=msh[q][h];
	}
	we=f[q]+w-q;
	qw+=max(0LL,min(T-f[q]+1,w-q+1));
	return make_pair(qw,we);
}
void rec(int q, int w, int qq, int ww){
	if(q>w) return;
	//cout<<q<<" "<<w<<" "<<qq<<" "<<ww<<"    "<<j<<endl;
	int mid=(q+w)/2;
	zx=0;c=f[i];
	P=cost(mid,mid);
	long long qw=a+3,we=0;
	for(ii=qq; ii<=/*min(w,ww)*/ww; ii++){
		if(ii!=mid){
			zx=(min(f[ii],P.second+1)<=T);
			P.first+=zx;
			P.second=min(f[ii],P.second+1);
		}
		if(qw>P.first+dp[ii+1][j-1]){
			qw=P.first+dp[ii+1][j-1];we=ii;
		}
	}
	dp[mid][j]=qw;
	rec(q,mid-1,qq,we);
	rec(mid+1,w,we,ww);
}
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>a>>k>>T;pas=a+3;
	for(i=1; i<=a; i++){
		cin>>f[i];
	}
	for(i=0; i<=a+1; i++){
		dp[i].resize(k+3);
	}
	for(i=0; i<=a+1; i++){
		for(j=0; j<=k+1; j++){
			dp[i][j]=a+3;
		}
	}
	for(i=1; i<=a; i++){
		m[f[i]-i]=1;
	}
	m[-1010000002]=1;zx=0;
	for(it=m.begin(); it!=m.end(); it++){
		zx++;
		(*it).second=zx;
	}
	for(i=1; i<=a; i++) F[i]=m[f[i]-i];
	F[a+1]=m[-1010000002];
	/*for(i=1; i<=a; i++){
		zx=0;c=f[i];
		for(ii=i; ii<=a; ii++){
			xc=(min(f[ii],c+1)<=T);
			zx+=xc;
			c=min(f[ii],c+1);
		}
		dp[i][0]=zx;
	}*/
	for(i=0; i<=a+2; i++){
		fen[i]=a+3;
	}
	dp[a+1][0]=0;
	upd(F[a+1],a+1);
	for(i=a; i>=1; i--){
		j=read(F[i]);
		if(i<=100000) msh[i][0]=j;
		dp[i][0]=dp[j][0];
		j--;
		//dp[i][0]+=max(0LL,min(f[i]+(j-i)-T+1,j-i+1));
		zx=max(0LL,min(T-f[i]+1,j-i+1));
		if(i<=100000) msh2[i][0]=zx;
		dp[i][0]+=zx;
		upd(F[i],i);
		//cout<<i<<" "<<j<<"   "<<dp[i][0]<<endl;
	}
	/*P=cost(1,2);
	cout<<P.first<<" "<<P.second<<endl;
	return 0;*/
	if(k==1){
		zx=0;c=f[1];
		for(ii=1; ii<a; ii++){
			xc=(min(f[ii],c+1)<=T);
			zx+=xc;
			pas=min(pas,zx+dp[ii+1][0]);
			c=min(f[ii],c+1);
		}
		cout<<pas;
		return 0;
	}
	for(j=1; j<=17; j++){
		for(i=1; i<=a; i++){
			msh[i][j]=msh[msh[i][j-1]][j-1];
			msh2[i][j]=msh2[i][j-1]+msh2[msh[i][j-1]][j-1];
		}
	}
	/*for(i=a; i>=1; i--){
		for(j=1; j<=k; j++){
			zx=0;c=f[i];
			for(ii=i; ii<=a; ii++){
				xc=(min(f[ii],c+1)<=T);
				zx+=xc;
				e=dp[ii+1][j-1]+zx;
				if(dp[i][j]>e) dp[i][j]=e;
				c=min(c+1,f[ii]);
			}
		}
	}*/
	for(j=1; j<=k; j++){
		rec(1,a,1,a);
	}
	cout<<dp[1][k];
	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...