Submission #530291

#TimeUsernameProblemLanguageResultExecution timeMemory
530291HowardGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms204 KiB
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n, mn = (int)1e9, ans;
	cin >> n;
	int a[n];
	vector<int> pos(n);
	vector<int> neg(n);
	for(int i = 0; i < n; i++) cin >> a[i];
	for(int i = 1; i < n; i++){
		if(a[i] > a[i-1]){
			pos[i] = pos[i-1];
			neg[i] = neg[i-1] + a[i] - a[i-1] + 1;
		}
		else if(a[i] < a[i-1]){
			pos[i] = pos[i-1] + a[i-1] - a[i] + 1;
			neg[i] = neg[i-1];
		}
		else{
			pos[i] = pos[i-1] + 1;
			neg[i] = neg[i-1] + 1;
		}
	}
	for(int k = 0; k < n; k++){
		ans = pos[k] + neg[n-k-1];
		mn = min(mn,ans);
	}
	cout << mn << endl;
	return 0;
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...