#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef long double ld; typedef pair<int, int> ii; typedef pair<ii, ii> iii; int MOD = 1e9 + 7; const ld E = 1e-9; #define null NULL #define ms(x) memset(x, 0, sizeof(x)) #ifndef LOCAL #define endl "\n" #endif #ifndef LONG_LONG_MAX #define LONG_LONG_MAX LLONG_MAX #endif #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define _read(x) freopen(x, "r", stdin) #define _write(x) freopen(x, "w", stdout) #define files(x) _read(x ".in"); _write(x ".out") #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL") #define output _write("output.txt") #define input _read("input.txt") #define prev time_prev #ifndef M_PI #define M_PI acos(-1) #endif #define remove tipa_remove #define next tipa_next #define left tipa_left #define right tipa_right #define y1 hello_world unsigned char ccc; bool _minus = false; template<typename T> inline T sqr(T t) { return (t * t); } inline void read(ll &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } inline bool read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') { if (ccc == '\n') return true; break; } if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; return false; } char wwww[19]; int kkkk; inline void write(ll y) { long long x = y; kkkk = 0; if (x < 0) { putchar('-'); x *= -1; } if (!x){ ++kkkk; wwww[kkkk] = '0'; }else while (x) { ++kkkk; wwww[kkkk] = char(x % 10 + '0'); x /= 10; } for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); } #ifdef LOCAL //#define __DEBUG #endif #ifdef __DEBUG #define dbg if(1) #else #define dbg if(0) #endif const int MAX = 1e5; int dp[MAX][2]; void solve() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; dp[i][0] = dp[i][1] = (int) 1e9; } dp[n][0] = dp[n][1] = (int) 1e9; dp[1][0] = dp[1][1] = 0; dp[2][0] = dp[2][1] = 1; if (v[0] < v[1]) { dp[2][1] = 0; } else { dp[2][1] = 1; } if (v[0] > v[1]) { dp[2][0] = 0; } else { dp[2][0] = 1; } for (int i = 3; i <= n; i++) { if (v[i - 2] < v[i - 1]) { dp[i][1] = dp[i - 1][0]; } if (v[i - 2] > v[i - 1]) { dp[i][0] = dp[i - 1][1]; } dp[i][0] = min(dp[i][0], dp[i - 2][0] + 1); dp[i][1] = min(dp[i][1], dp[i - 2][1] + 1); } cout << min(dp[n][0], dp[n][1]) << endl; } int main() { sync; srand((unsigned int) time(NULL)); cout.precision(10); cout << fixed; int t = 1; while(t--){ solve(); } }
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 152 153 154 155 156 157 158 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef long double ld; typedef pair<int, int> ii; typedef pair<ii, ii> iii; int MOD = 1e9 + 7; const ld E = 1e-9; #define null NULL #define ms(x) memset(x, 0, sizeof(x)) #ifndef LOCAL #define endl "\n" #endif #ifndef LONG_LONG_MAX #define LONG_LONG_MAX LLONG_MAX #endif #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define _read(x) freopen(x, "r", stdin) #define _write(x) freopen(x, "w", stdout) #define files(x) _read(x ".in"); _write(x ".out") #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL") #define output _write("output.txt") #define input _read("input.txt") #define prev time_prev #ifndef M_PI #define M_PI acos(-1) #endif #define remove tipa_remove #define next tipa_next #define left tipa_left #define right tipa_right #define y1 hello_world unsigned char ccc; bool _minus = false; template<typename T> inline T sqr(T t) { return (t * t); } inline void read(ll &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } inline bool read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') { if (ccc == '\n') return true; break; } if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; return false; } char wwww[19]; int kkkk; inline void write(ll y) { long long x = y; kkkk = 0; if (x < 0) { putchar('-'); x *= -1; } if (!x){ ++kkkk; wwww[kkkk] = '0'; }else while (x) { ++kkkk; wwww[kkkk] = char(x % 10 + '0'); x /= 10; } for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); } #ifdef LOCAL //#define __DEBUG #endif #ifdef __DEBUG #define dbg if(1) #else #define dbg if(0) #endif const int MAX = 1e5; int dp[MAX][2]; void solve() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; dp[i][0] = dp[i][1] = (int) 1e9; } dp[n][0] = dp[n][1] = (int) 1e9; dp[1][0] = dp[1][1] = 0; dp[2][0] = dp[2][1] = 1; if (v[0] < v[1]) { dp[2][1] = 0; } else { dp[2][1] = 1; } if (v[0] > v[1]) { dp[2][0] = 0; } else { dp[2][0] = 1; } for (int i = 3; i <= n; i++) { if (v[i - 2] < v[i - 1]) { dp[i][1] = dp[i - 1][0]; } if (v[i - 2] > v[i - 1]) { dp[i][0] = dp[i - 1][1]; } dp[i][0] = min(dp[i][0], dp[i - 2][0] + 1); dp[i][1] = min(dp[i][1], dp[i - 2][1] + 1); } cout << min(dp[n][0], dp[n][1]) << endl; } int main() { sync; srand((unsigned int) time(NULL)); cout.precision(10); cout << fixed; int t = 1; while(t--){ solve(); } } |