Submission #913257

#TimeUsernameProblemLanguageResultExecution timeMemory
913257goodspeed0208Growing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
27 ms4088 KiB
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#define int long long 
using namespace std;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int>v(n);
	for (int i = 0 ; i < n ; i++) cin >> v[i];
	int left = 0, right = n-1, ans = 0;
	while (v[left+1] > v[left]) left++;
	while (v[right-1] > v[right]) right--;
	int lg, rg;
	lg = v[left] - v[left+1];
	rg = v[right] - v[right-1];
	
	while (left < right) {
		if (lg == rg) {
			ans += lg + 1;
			left++, right--;
			while (v[left+1] > v[left]) left++;
			while (v[right-1] > v[right]) right--;
			lg = v[left] - v[left+1];
			rg = v[right] - v[right-1];
		} else if (lg > rg) {
			ans += rg + 1;
			lg -= (rg+1);
			right--;
			while (v[right-1] > v[right]) right--;
			rg = v[right] - v[right-1];
		} else if (rg > lg) {
			ans += lg + 1;
			rg -= (lg+1);
			left++;
			while (v[left+1] > v[left]) left++;
			lg = v[left] - v[left+1];
		}
		//cout << ans <<" " << left << " " << right<< "\n";
	}
	cout << ans << "\n";
}













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