#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0); int n; cin >> n; int a[n]; int b[n]; for(int i = 0; i < n; i++){ cin >> a[i]; b[i] = a[i]; } if(n == 1){ cout << 0; return 0; } int prev,next,curr; int res = 0; bool ros = !(a[0] < a[1]); for(int i = 1; i < n; i++){ ros = !ros; prev = a[i-1]; next = i == n-1 ? prev : a[i+1]; curr = a[i]; if(prev == curr || (prev < curr && !ros) || (prev > curr && ros)){ if(ros){ a[i] = max(prev,next)+1; }else{ a[i] = min(prev,next)-1; } res++; } } int res2 = 0; bool ros2 = b[0] < b[1]; for(int i = 1; i < n; i++){ ros2 = !ros2; prev = b[i-1]; next = i == n-1 ? prev : b[i+1]; curr = b[i]; if(prev == curr || (prev < curr && !ros2) || (prev > curr && ros2)){ if(ros2){ b[i] = max(prev,next)+1; }else{ b[i] = min(prev,next)-1; } res2++; } } cout << min(res,res2) << endl; 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 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0); int n; cin >> n; int a[n]; int b[n]; for(int i = 0; i < n; i++){ cin >> a[i]; b[i] = a[i]; } if(n == 1){ cout << 0; return 0; } int prev,next,curr; int res = 0; bool ros = !(a[0] < a[1]); for(int i = 1; i < n; i++){ ros = !ros; prev = a[i-1]; next = i == n-1 ? prev : a[i+1]; curr = a[i]; if(prev == curr || (prev < curr && !ros) || (prev > curr && ros)){ if(ros){ a[i] = max(prev,next)+1; }else{ a[i] = min(prev,next)-1; } res++; } } int res2 = 0; bool ros2 = b[0] < b[1]; for(int i = 1; i < n; i++){ ros2 = !ros2; prev = b[i-1]; next = i == n-1 ? prev : b[i+1]; curr = b[i]; if(prev == curr || (prev < curr && !ros2) || (prev > curr && ros2)){ if(ros2){ b[i] = max(prev,next)+1; }else{ b[i] = min(prev,next)-1; } res2++; } } cout << min(res,res2) << endl; return 0; } |