#include <bits/stdc++.h> using namespace std; const int N = 50005; int n; int tab[N]; int dp[2][N]; // 0 -> nie rob nic, 1 -> uwal int solve() { dp[1][0] = 1; for (int i = 1; i < n; i++) { if (((tab[i] > tab[i - 1]) == (i & 1)) & (tab[i] != tab[i - 1])) { dp[0][i] = dp[0][i - 1]; } else { dp[0][i] = N; } dp[0][i] = min(dp[0][i], dp[1][i - 1]); dp[1][i] = min(dp[0][i - 1], dp[1][i - 1]) + 1; } return min(dp[0][n - 1], dp[1][n - 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> tab[i]; } int res = solve(); for (int i = 0; i < n; i++) { tab[i] *= -1; } res = min(res, solve()); cout << res << "\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 36 37 38 | #include <bits/stdc++.h> using namespace std; const int N = 50005; int n; int tab[N]; int dp[2][N]; // 0 -> nie rob nic, 1 -> uwal int solve() { dp[1][0] = 1; for (int i = 1; i < n; i++) { if (((tab[i] > tab[i - 1]) == (i & 1)) & (tab[i] != tab[i - 1])) { dp[0][i] = dp[0][i - 1]; } else { dp[0][i] = N; } dp[0][i] = min(dp[0][i], dp[1][i - 1]); dp[1][i] = min(dp[0][i - 1], dp[1][i - 1]) + 1; } return min(dp[0][n - 1], dp[1][n - 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> tab[i]; } int res = solve(); for (int i = 0; i < n; i++) { tab[i] *= -1; } res = min(res, solve()); cout << res << "\n"; } |