#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> arr1(n); vector<int> arr2(n); for(int i = 0; i < n; i++){ cin >> arr1[i]; } arr2 = arr1; int res1= 0, res2 = 0; bool shouldIncrease = true; for(int i = 0; i < n-1; i++){ if(shouldIncrease && arr1[i] >= arr1[i+1]){ res1++; arr1[i+1] = INT_MAX; }else if(!shouldIncrease && arr1[i] <= arr1[i+1]){ res1++; arr1[i+1] = INT_MIN; }else{ } shouldIncrease = !shouldIncrease; } shouldIncrease = false; for(int i = 0; i < n-1; i++){ if(shouldIncrease && arr2[i] >= arr2[i+1]){ res2++; arr2[i+1] = INT_MAX; }else if(!shouldIncrease && arr2[i] <= arr2[i+1]){ res2++; arr2[i+1] = INT_MIN; } shouldIncrease = !shouldIncrease; } cout << min(res1,res2)<< '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> arr1(n); vector<int> arr2(n); for(int i = 0; i < n; i++){ cin >> arr1[i]; } arr2 = arr1; int res1= 0, res2 = 0; bool shouldIncrease = true; for(int i = 0; i < n-1; i++){ if(shouldIncrease && arr1[i] >= arr1[i+1]){ res1++; arr1[i+1] = INT_MAX; }else if(!shouldIncrease && arr1[i] <= arr1[i+1]){ res1++; arr1[i+1] = INT_MIN; }else{ } shouldIncrease = !shouldIncrease; } shouldIncrease = false; for(int i = 0; i < n-1; i++){ if(shouldIncrease && arr2[i] >= arr2[i+1]){ res2++; arr2[i+1] = INT_MAX; }else if(!shouldIncrease && arr2[i] <= arr2[i+1]){ res2++; arr2[i+1] = INT_MIN; } shouldIncrease = !shouldIncrease; } cout << min(res1,res2)<< '\n'; return 0; } |