#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 50004; constexpr int oo = 1000000000; int c[MAXN]; int ct[MAXN]; int main(){ int n; cin >> n; for(int i = 1; i <= n; i++){ cin >> c[i]; } int r1 = 0; ct[1] = c[1]; for(int i = 2; i <= n; i++){ ct[i] = c[i]; if ( ct[i-1] == ct[i] || ((i % 2) ^ (ct[i-1] < ct[i]) )){ r1++; ct[i] = 2*oo*(i%2) - oo; } } int r2 = 0; ct[1] = c[1]; for(int i = 2; i <= n; i++){ ct[i] = c[i]; if ( ct[i-1] == ct[i] || (!(i % 2) ^ (ct[i-1] < ct[i]) )){ r2++; ct[i] = 2*oo*(!(i%2)) - oo; } } cout << min(r1, r2) << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 50004; constexpr int oo = 1000000000; int c[MAXN]; int ct[MAXN]; int main(){ int n; cin >> n; for(int i = 1; i <= n; i++){ cin >> c[i]; } int r1 = 0; ct[1] = c[1]; for(int i = 2; i <= n; i++){ ct[i] = c[i]; if ( ct[i-1] == ct[i] || ((i % 2) ^ (ct[i-1] < ct[i]) )){ r1++; ct[i] = 2*oo*(i%2) - oo; } } int r2 = 0; ct[1] = c[1]; for(int i = 2; i <= n; i++){ ct[i] = c[i]; if ( ct[i-1] == ct[i] || (!(i % 2) ^ (ct[i-1] < ct[i]) )){ r2++; ct[i] = 2*oo*(!(i%2)) - oo; } } cout << min(r1, r2) << '\n'; } |