#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> w(n, 0); for (auto &x : w) std::cin >> x; const int MAX = 1e9; // a_0 < a_1 int res1 = 0; std::vector<int> v = w; for (int i = 0; i < n-1; i++) { if (i % 2 == 0 && v[i] >= v[i+1]) { res1++; v[i+1] = MAX; } else if (i % 2 == 1 && v[i] <= v[i+1]) { res1++; v[i+1] = -MAX; } } // a_0 > a_1 int res2 = 0; v = w; for (int i = 0; i < n-1; i++) { if (i % 2 == 0 && v[i] <= v[i+1]) { res2++; v[i+1] = -MAX; } else if (i % 2 == 1 && v[i] >= v[i+1]) { res2++; v[i+1] = MAX; } } std::cout << std::min(res1, res2) << "\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 | #include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n; std::cin >> n; std::vector<int> w(n, 0); for (auto &x : w) std::cin >> x; const int MAX = 1e9; // a_0 < a_1 int res1 = 0; std::vector<int> v = w; for (int i = 0; i < n-1; i++) { if (i % 2 == 0 && v[i] >= v[i+1]) { res1++; v[i+1] = MAX; } else if (i % 2 == 1 && v[i] <= v[i+1]) { res1++; v[i+1] = -MAX; } } // a_0 > a_1 int res2 = 0; v = w; for (int i = 0; i < n-1; i++) { if (i % 2 == 0 && v[i] <= v[i+1]) { res2++; v[i+1] = -MAX; } else if (i % 2 == 1 && v[i] >= v[i+1]) { res2++; v[i+1] = MAX; } } std::cout << std::min(res1, res2) << "\n"; } |