#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 50500; int n, a[N]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> a[i]; int res = n; rep(f, 0, 1) { int cur = 0; int cnt = 1; rep(i, 2, n) { if (a[i - 1] == a[i] || (f ^ (i % 2)) != (a[i - 1] < a[i])) { cnt++; } else { cur += cnt / 2; cnt = 1; } } cur += cnt / 2; res = min(res, cur); } cout << res << "\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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 50500; int n, a[N]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> a[i]; int res = n; rep(f, 0, 1) { int cur = 0; int cnt = 1; rep(i, 2, n) { if (a[i - 1] == a[i] || (f ^ (i % 2)) != (a[i - 1] < a[i])) { cnt++; } else { cur += cnt / 2; cnt = 1; } } cur += cnt / 2; res = min(res, cur); } cout << res << "\n"; return 0; } |