#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int& i : a) cin >> i; vector<int> b = a; int ans1 = 0; for (int i = 0; i < n-1; i++) { if (i % 2) { if (a[i] <= a[i+1]) { a[i+1] = -1e8; ans1++; } } else { if (a[i] >= a[i+1]) { a[i+1] = 1e8; ans1++; } } } int ans2 = 0; a = b; for (int i = 0; i < n-1; i++) { if (i % 2) { if (a[i] >= a[i+1]) { a[i+1] = 1e8; ans2++; } } else { if (a[i] <= a[i+1]) { a[i+1] = -1e8; ans2++; } } } cout << min(ans1, ans2) << '\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 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int& i : a) cin >> i; vector<int> b = a; int ans1 = 0; for (int i = 0; i < n-1; i++) { if (i % 2) { if (a[i] <= a[i+1]) { a[i+1] = -1e8; ans1++; } } else { if (a[i] >= a[i+1]) { a[i+1] = 1e8; ans1++; } } } int ans2 = 0; a = b; for (int i = 0; i < n-1; i++) { if (i % 2) { if (a[i] >= a[i+1]) { a[i+1] = 1e8; ans2++; } } else { if (a[i] <= a[i+1]) { a[i+1] = -1e8; ans2++; } } } cout << min(ans1, ans2) << '\n'; return 0; } |