#include <bits/stdc++.h> using namespace std; const int MAXN = 50010; int A[MAXN]; int DP[MAXN][2]; int solve(int n) { int out = 0; vector<int> DP(n + 1); DP[1] = 0; int s = 1; for (int i = 2; i <= n; i++) { if (A[i] * s > A[i - 1] * s) { DP[i] = DP[i - 1]; } else if (i == 2) { DP[i] = 1; } else { DP[i] = 1 + min(DP[i - 1], DP[i - 2]); } s = -s; } return DP[n]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", A + i); for (int i = 2; i <= n; i++) { for (int j = 0; j < 2; j++) { int s = j == 0 ? 1 : -1; if (A[i] * s > A[i - 1] * s) { DP[i][j] = DP[i - 1][1 - j]; } else if (i == 2) { DP[i][j] = 1; } else { DP[i][j] = 1 + min(DP[i - 1][1 - j], DP[i - 2][j]); } } } printf("%d\n", min(DP[n][0], DP[n][1])); }
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 42 43 44 45 46 47 48 49 50 51 | #include <bits/stdc++.h> using namespace std; const int MAXN = 50010; int A[MAXN]; int DP[MAXN][2]; int solve(int n) { int out = 0; vector<int> DP(n + 1); DP[1] = 0; int s = 1; for (int i = 2; i <= n; i++) { if (A[i] * s > A[i - 1] * s) { DP[i] = DP[i - 1]; } else if (i == 2) { DP[i] = 1; } else { DP[i] = 1 + min(DP[i - 1], DP[i - 2]); } s = -s; } return DP[n]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", A + i); for (int i = 2; i <= n; i++) { for (int j = 0; j < 2; j++) { int s = j == 0 ? 1 : -1; if (A[i] * s > A[i - 1] * s) { DP[i][j] = DP[i - 1][1 - j]; } else if (i == 2) { DP[i][j] = 1; } else { DP[i][j] = 1 + min(DP[i - 1][1 - j], DP[i - 2][j]); } } } printf("%d\n", min(DP[n][0], DP[n][1])); } |