제출 #1154654

#제출 시각아이디문제언어결과실행 시간메모리
1154654NurislamGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
0 / 100
1095 ms328 KiB
#include <bits/stdc++.h>
using namespace std;

//#define int long long
//#define all(x) x.begin(),x.end()
//#define rall(x) x.rbegin(),x.rend()
//#define ff first
//#define ss second
//#define pb push_back
//template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; }
//template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; }
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng)

const int inf = 1e6, mod = 1e9 + 7;

void solve(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int &i : a)cin >> i;
	
	int l = 1, r = n-2, ans = 0;
	
	while(1){
		while(l < n){
			if(a[l-1] < a[l]) l ++ ;
			else break;
			if(l < r)a[l] += ans;
		};
		while(r >= 0){
			if(a[r+1] < a[r]) r -- ;
			else break;
			if(r > l)a[r] += ans;
		};
		
		if(l == r+1 && a[l] == a[r]){ans ++ ;break;}
		if(l > r)break;
		
		
		//cout << l << ' ' << r << '\n';
		int lf = a[l-1] - a[l] + 1;
		int ri = a[r+1] - a[r] + 1;
		if(lf < ri){
			a[l] += lf;
			a[r] += lf;
			ans += lf;
			//cout << lf << '\n';
		}else{
			a[l] += ri;
			a[r] += ri;
			ans += ri;
			//cout << ri << '\n';
		};
	};
	//for(int i :a)cout << i << ' ';
	//cout << '\n';
	cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int tt = 1;
    //cin >> tt;
    while(tt--){
        solve();
    };
}














#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...