#include <bits/stdc++.h> using namespace std; constexpr int INF = 1'000'000'000; int calc(vector<int> a) { int result = 0; for (int i = 1; i < (int) a.size(); i++) { if (i % 2 == 0) { if (!(a[i] > a[i - 1])) { a[i] = INF; result++; } } else { if (!(a[i] < a[i - 1])) { a[i] = -INF; result++; } } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n), a_negative(n); for (int i = 0; i < n; i++) { cin >> a[i]; a_negative[i] = -a[i]; } cout << min(calc(a), calc(a_negative)) << '\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 | #include <bits/stdc++.h> using namespace std; constexpr int INF = 1'000'000'000; int calc(vector<int> a) { int result = 0; for (int i = 1; i < (int) a.size(); i++) { if (i % 2 == 0) { if (!(a[i] > a[i - 1])) { a[i] = INF; result++; } } else { if (!(a[i] < a[i - 1])) { a[i] = -INF; result++; } } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n), a_negative(n); for (int i = 0; i < n; i++) { cin >> a[i]; a_negative[i] = -a[i]; } cout << min(calc(a), calc(a_negative)) << '\n'; return 0; } |