#include<bits/stdc++.h> using namespace std; constexpr int inf = 10000001; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> in(n); for (int i = 0; i < n; i++) cin >> in[i]; int ans = inf; for (int dir : {0, 1}) { vector<int> a(in); int changes = 0; for (int i = 1; i < n; i++) { bool should_go_up = bool((i + dir) % 2); if (should_go_up and a[i] <= a[i - 1]) { a[i] = inf; changes++; } if (not should_go_up and a[i] >= a[i - 1]) { a[i] = -inf; changes++; } } ans = min(ans, changes); } cout << ans << "\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 | #include<bits/stdc++.h> using namespace std; constexpr int inf = 10000001; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> in(n); for (int i = 0; i < n; i++) cin >> in[i]; int ans = inf; for (int dir : {0, 1}) { vector<int> a(in); int changes = 0; for (int i = 1; i < n; i++) { bool should_go_up = bool((i + dir) % 2); if (should_go_up and a[i] <= a[i - 1]) { a[i] = inf; changes++; } if (not should_go_up and a[i] >= a[i - 1]) { a[i] = -inf; changes++; } } ans = min(ans, changes); } cout << ans << "\n"; } |