#include <algorithm>
#include <cstdio>
using namespace std;
const int FIRSTNEG = 0;
const int FIRSTPOS = 1;
const int NCHANGD = 0;
const int CHANGED = 1;
const int maxN = 5e4 + 10;
int dp[maxN][2][2];
int t[maxN];
void calc(int i, int first) {
bool should_be_pos = (i % 2 == 0) ^ (first == FIRSTPOS);
bool both_unchanged_possible = (should_be_pos && t[i] > t[i - 1]) ||
(!should_be_pos && t[i] < t[i - 1]);
dp[i][first][CHANGED] =
1 + min(dp[i - 1][first][CHANGED], dp[i - 1][first][NCHANGD]);
dp[i][first][NCHANGD] = dp[i - 1][first][CHANGED];
if (both_unchanged_possible)
dp[i][first][NCHANGD] = dp[i - 1][first][NCHANGD];
}
int main() {
int N;
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%d", t + i);
for (int i = 0; i < 2; ++i)
dp[0][i][CHANGED] = 1;
for (int i = 1; i < N; ++i) {
calc(i, FIRSTNEG);
calc(i, FIRSTPOS);
}
int result = dp[N - 1][FIRSTNEG][CHANGED];
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
result = min(result, dp[N - 1][i][j]);
printf("%d\n", result);
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 42 43 44 45 | #include <algorithm> #include <cstdio> using namespace std; const int FIRSTNEG = 0; const int FIRSTPOS = 1; const int NCHANGD = 0; const int CHANGED = 1; const int maxN = 5e4 + 10; int dp[maxN][2][2]; int t[maxN]; void calc(int i, int first) { bool should_be_pos = (i % 2 == 0) ^ (first == FIRSTPOS); bool both_unchanged_possible = (should_be_pos && t[i] > t[i - 1]) || (!should_be_pos && t[i] < t[i - 1]); dp[i][first][CHANGED] = 1 + min(dp[i - 1][first][CHANGED], dp[i - 1][first][NCHANGD]); dp[i][first][NCHANGD] = dp[i - 1][first][CHANGED]; if (both_unchanged_possible) dp[i][first][NCHANGD] = dp[i - 1][first][NCHANGD]; } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%d", t + i); for (int i = 0; i < 2; ++i) dp[0][i][CHANGED] = 1; for (int i = 1; i < N; ++i) { calc(i, FIRSTNEG); calc(i, FIRSTPOS); } int result = dp[N - 1][FIRSTNEG][CHANGED]; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) result = min(result, dp[N - 1][i][j]); printf("%d\n", result); return 0; } |
English