#include <cstdio> #include <algorithm> using namespace std; int a[50001]; int b[50001]; const int CURRENT_CURRENT = 0; const int CURRENT_LOWEST = 1; const int CURRENT_HIGHEST = 2; const int LOWEST_CURRENT = 3; const int LOWEST_HIGHEST = 4; const int HIGHEST_CURRENT = 5; const int HIGHEST_LOWEST = 6; const int INF = 1000000001; int dp[7]; int dp2[7]; //#include <ctime> int main() { // time_t t = clock(); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); b[i] = a[i]; } dp2[CURRENT_CURRENT] = INF; if (a[0] != a[1]) dp2[CURRENT_CURRENT] = 0; dp2[CURRENT_LOWEST] = 1; dp2[CURRENT_HIGHEST] = 1; dp2[LOWEST_CURRENT] = 1; dp2[LOWEST_HIGHEST] = 2; dp2[HIGHEST_CURRENT] = 1; dp2[HIGHEST_LOWEST] = 2; for (int i = 2; i < n; i++) { dp[CURRENT_CURRENT] = INF; if (a[i - 2] < a[i - 1] && a[i - 1] > a[i]) dp[CURRENT_CURRENT] = min(dp2[CURRENT_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 2] > a[i - 1] && a[i - 1] < a[i]) dp[CURRENT_CURRENT] = min(dp2[CURRENT_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 1] > a[i]) dp[CURRENT_CURRENT] = min(dp2[LOWEST_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 1] < a[i]) dp[CURRENT_CURRENT] = min(dp2[HIGHEST_CURRENT], dp[CURRENT_CURRENT]); dp[CURRENT_LOWEST] = INF; if (a[i - 2] < a[i - 1]) dp[CURRENT_LOWEST] = min(dp2[CURRENT_CURRENT] + 1, dp[CURRENT_LOWEST]); dp[CURRENT_LOWEST] = min(dp2[LOWEST_CURRENT] + 1, dp[CURRENT_LOWEST]); dp[CURRENT_HIGHEST] = INF; if (a[i - 2] > a[i - 1]) dp[CURRENT_HIGHEST] = min(dp2[CURRENT_CURRENT] + 1, dp[CURRENT_HIGHEST]); dp[CURRENT_HIGHEST] = min(dp2[HIGHEST_CURRENT] + 1, dp[CURRENT_HIGHEST]); dp[LOWEST_CURRENT] = INF; dp[LOWEST_CURRENT] = min(dp2[CURRENT_LOWEST], dp[LOWEST_CURRENT]); dp[LOWEST_CURRENT] = min(dp2[HIGHEST_LOWEST], dp[LOWEST_CURRENT]); dp[LOWEST_HIGHEST] = INF; dp[LOWEST_HIGHEST] = min(dp2[CURRENT_LOWEST] + 1, dp[LOWEST_HIGHEST]); dp[LOWEST_HIGHEST] = min(dp2[HIGHEST_LOWEST] + 1, dp[LOWEST_HIGHEST]); dp[HIGHEST_CURRENT] = INF; dp[HIGHEST_CURRENT] = min(dp2[CURRENT_HIGHEST], dp[HIGHEST_CURRENT]); dp[HIGHEST_CURRENT] = min(dp2[LOWEST_HIGHEST], dp[HIGHEST_CURRENT]); dp[HIGHEST_LOWEST] = INF; dp[HIGHEST_LOWEST] = min(dp2[CURRENT_HIGHEST] + 1, dp[HIGHEST_LOWEST]); dp[HIGHEST_LOWEST] = min(dp2[LOWEST_HIGHEST] + 1, dp[HIGHEST_LOWEST]); for (int j = 0; j < 7; j++) dp2[j] = dp[j]; } int ret = INF; for (int i = 0; i < 7; i++) { ret = min(ret, dp[i]); } int m1 = 0; for (int i = 0; i + 1 < n; i++) { if (i % 2 == 0) { if (a[i] >= a[i + 1]) { a[i + 1] = 1000000000; m1++; } } else { if (a[i] <= a[i + 1]) { a[i + 1] = -1000000000; m1++; } } } int m2 = 0; for (int i = 0; i + 1 < n; i++) { if (i % 2 == 0) { if (b[i] <= b[i + 1]) { b[i + 1] = -1000000000; m2++; } } else { if (b[i] >= b[i + 1]) { b[i + 1] = 1000000000; m2++; } } } int ret2 = min(m1, m2); //assert(ret == ret2); ret = min(ret, ret2); printf("%d\n", ret); //printf("%lld\n", clock() - t); 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <cstdio> #include <algorithm> using namespace std; int a[50001]; int b[50001]; const int CURRENT_CURRENT = 0; const int CURRENT_LOWEST = 1; const int CURRENT_HIGHEST = 2; const int LOWEST_CURRENT = 3; const int LOWEST_HIGHEST = 4; const int HIGHEST_CURRENT = 5; const int HIGHEST_LOWEST = 6; const int INF = 1000000001; int dp[7]; int dp2[7]; //#include <ctime> int main() { // time_t t = clock(); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); b[i] = a[i]; } dp2[CURRENT_CURRENT] = INF; if (a[0] != a[1]) dp2[CURRENT_CURRENT] = 0; dp2[CURRENT_LOWEST] = 1; dp2[CURRENT_HIGHEST] = 1; dp2[LOWEST_CURRENT] = 1; dp2[LOWEST_HIGHEST] = 2; dp2[HIGHEST_CURRENT] = 1; dp2[HIGHEST_LOWEST] = 2; for (int i = 2; i < n; i++) { dp[CURRENT_CURRENT] = INF; if (a[i - 2] < a[i - 1] && a[i - 1] > a[i]) dp[CURRENT_CURRENT] = min(dp2[CURRENT_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 2] > a[i - 1] && a[i - 1] < a[i]) dp[CURRENT_CURRENT] = min(dp2[CURRENT_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 1] > a[i]) dp[CURRENT_CURRENT] = min(dp2[LOWEST_CURRENT], dp[CURRENT_CURRENT]); if (a[i - 1] < a[i]) dp[CURRENT_CURRENT] = min(dp2[HIGHEST_CURRENT], dp[CURRENT_CURRENT]); dp[CURRENT_LOWEST] = INF; if (a[i - 2] < a[i - 1]) dp[CURRENT_LOWEST] = min(dp2[CURRENT_CURRENT] + 1, dp[CURRENT_LOWEST]); dp[CURRENT_LOWEST] = min(dp2[LOWEST_CURRENT] + 1, dp[CURRENT_LOWEST]); dp[CURRENT_HIGHEST] = INF; if (a[i - 2] > a[i - 1]) dp[CURRENT_HIGHEST] = min(dp2[CURRENT_CURRENT] + 1, dp[CURRENT_HIGHEST]); dp[CURRENT_HIGHEST] = min(dp2[HIGHEST_CURRENT] + 1, dp[CURRENT_HIGHEST]); dp[LOWEST_CURRENT] = INF; dp[LOWEST_CURRENT] = min(dp2[CURRENT_LOWEST], dp[LOWEST_CURRENT]); dp[LOWEST_CURRENT] = min(dp2[HIGHEST_LOWEST], dp[LOWEST_CURRENT]); dp[LOWEST_HIGHEST] = INF; dp[LOWEST_HIGHEST] = min(dp2[CURRENT_LOWEST] + 1, dp[LOWEST_HIGHEST]); dp[LOWEST_HIGHEST] = min(dp2[HIGHEST_LOWEST] + 1, dp[LOWEST_HIGHEST]); dp[HIGHEST_CURRENT] = INF; dp[HIGHEST_CURRENT] = min(dp2[CURRENT_HIGHEST], dp[HIGHEST_CURRENT]); dp[HIGHEST_CURRENT] = min(dp2[LOWEST_HIGHEST], dp[HIGHEST_CURRENT]); dp[HIGHEST_LOWEST] = INF; dp[HIGHEST_LOWEST] = min(dp2[CURRENT_HIGHEST] + 1, dp[HIGHEST_LOWEST]); dp[HIGHEST_LOWEST] = min(dp2[LOWEST_HIGHEST] + 1, dp[HIGHEST_LOWEST]); for (int j = 0; j < 7; j++) dp2[j] = dp[j]; } int ret = INF; for (int i = 0; i < 7; i++) { ret = min(ret, dp[i]); } int m1 = 0; for (int i = 0; i + 1 < n; i++) { if (i % 2 == 0) { if (a[i] >= a[i + 1]) { a[i + 1] = 1000000000; m1++; } } else { if (a[i] <= a[i + 1]) { a[i + 1] = -1000000000; m1++; } } } int m2 = 0; for (int i = 0; i + 1 < n; i++) { if (i % 2 == 0) { if (b[i] <= b[i + 1]) { b[i + 1] = -1000000000; m2++; } } else { if (b[i] >= b[i + 1]) { b[i + 1] = 1000000000; m2++; } } } int ret2 = min(m1, m2); //assert(ret == ret2); ret = min(ret, ret2); printf("%d\n", ret); //printf("%lld\n", clock() - t); return 0; } |