Submission #1327982

#TimeUsernameProblemLanguageResultExecution timeMemory
1327982joacruGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
0 / 100
19 ms572 KiB
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>

#define forn(i,n) for(int i=0;i<(int)n;++i)
#define fort(i,n) for(int i=0;i<=(int)n;++i)
#define fori(i,a,n) for(int i=a;i<(int)n;++i)
#define forit(i,a,n) for(int i=a;i<=(int)n;++i)

#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()

using namespace std;

template<typename T>
ostream &operator<<(ostream &os, vector<T> v){
	os<<"[";
	forn(i,SZ(v)){
		if(i) os<<", ";
		os<<v[i];
	}
	os<<"]";
	return os;
}

typedef long long ll;

const ll INF = 1000000000000000000;

struct Node{
	ll ops = 0;
	int act = 0, val = 0;
};

vector<Node> get(const vector<int> &a){
	int n = SZ(a);
	vector<Node> ret(n + 1);
	int active = 0, last = 0;
	forn(i,n){
		int need = max(0, last - a[i] + 1);
		ll ops = ret[i].ops + max(0, need - active);
		active = need;
		last = a[i]+need;
		ret[i+1] = {ops, active, last};
	}
	return ret;
}

void solve(){
	int n;
	cin>>n;
	vector<int> a(n);
	for(int &x: a) cin>>x;
	vector<Node> fromLeft, fromRight;
	forn(_,2){
		fromLeft = get(a);
		swap(fromLeft, fromRight);
		reverse(ALL(a));
	}
	reverse(ALL(fromRight));
	forn(_,2){
		fort(i,n){
			cerr<<"("<<fromLeft[i].ops<<","<<fromLeft[i].act<<","<<fromLeft[i].val<<")    ";
		}
		cerr<<endl;
		swap(fromLeft, fromRight);
	}
	ll ans = INF;
	forn(i,n){ // pone este valor como central
		ll aux = fromLeft[i].ops + fromRight[i+1].ops; // operaciones iniciales
		int val = max(fromLeft[i].val, fromRight[i+1].val)+1; // valor mínimo que debo tener
		int minAct = min(fromLeft[i].act, fromRight[i+1].act); // cuanto me puedo ahorrar
		int maxAct = max(fromLeft[i].act, fromRight[i+1].act); // cuanto puedo aprovechar
		aux -= minAct;
		int cur = a[i] + maxAct; // crezco lo mas que puedo
		aux += max(0,val-cur);
		ans = min(ans, aux);
	}
	ans = min(ans, fromLeft[n].ops);
	ans = min(ans, fromRight[0].ops);
	cout<<ans<<"\n";
}

int main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	#ifdef LOCAL
	freopen("A.in", "r", stdin);
	int tcs;
	cin>>tcs;
	while(tcs--)
	#endif
	solve();
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...