#include <bits/stdc++.h> typedef long long ll; using namespace std; const ll inf = 1e18 + 7; const ll mod = 1e9 + 7; ll n , m , k , ans = 0 , q; ll dp[50005][2][2]; // ifstream in , out; int main() { ios_base::sync_with_stdio(0); cin.tie(0); /* for (int t = 5; t < 1000; t++) { string sin = "orn" , sout = "orn" , st = ""; ll kop = t; while (kop > 0) { st += char(kop % 10 + 48); kop /= 10; } if (st.size() == 0)st = "0"; reverse(st.begin() , st.end()); sin += st; sout += st; sin += ".in"; sout += ".out"; cout << sin << endl; in.open(sin); out.open(sout); */ cin >> n; vector<ll>vec(n + 1); for (int i = 1; i <= n; i++) { cin >> vec[i]; } dp[1][0][0] = dp[1][1][0] = 0; dp[1][0][1] = dp[1][1][1] = 1; for (int i = 2; i <= n; i++) { dp[i][0][0] = dp[i - 1][1][1]; if (vec[i - 1] > vec[i])dp[i][0][0] = min(dp[i][0][0] , dp[i - 1][1][0]); dp[i][0][1] = min(dp[i - 1][1][0] , dp[i - 1][1][1]) + 1; dp[i][1][0] = dp[i - 1][0][1]; if (vec[i - 1] < vec[i])dp[i][1][0] = min(dp[i][1][0] , dp[i - 1][0][0]); dp[i][1][1] = min(dp[i - 1][0][0] , dp[i - 1][0][1]) + 1; } ans = min({dp[n][0][0] , dp[n][0][1] , dp[n][1][0] , dp[n][1][1]}); cout << ans; /* ll real_ans; out >> real_ans; cout << real_ans << ' ' << ans << endl; if (real_ans != ans) { cout << n << endl; for (int i = 1; i <= n; i++) cout << vec[i] << ' '; cout << endl; cout << ans << ' ' << real_ans; return 0; } in.close(); out.close(); } */ } /* */
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 | #include <bits/stdc++.h> typedef long long ll; using namespace std; const ll inf = 1e18 + 7; const ll mod = 1e9 + 7; ll n , m , k , ans = 0 , q; ll dp[50005][2][2]; // ifstream in , out; int main() { ios_base::sync_with_stdio(0); cin.tie(0); /* for (int t = 5; t < 1000; t++) { string sin = "orn" , sout = "orn" , st = ""; ll kop = t; while (kop > 0) { st += char(kop % 10 + 48); kop /= 10; } if (st.size() == 0)st = "0"; reverse(st.begin() , st.end()); sin += st; sout += st; sin += ".in"; sout += ".out"; cout << sin << endl; in.open(sin); out.open(sout); */ cin >> n; vector<ll>vec(n + 1); for (int i = 1; i <= n; i++) { cin >> vec[i]; } dp[1][0][0] = dp[1][1][0] = 0; dp[1][0][1] = dp[1][1][1] = 1; for (int i = 2; i <= n; i++) { dp[i][0][0] = dp[i - 1][1][1]; if (vec[i - 1] > vec[i])dp[i][0][0] = min(dp[i][0][0] , dp[i - 1][1][0]); dp[i][0][1] = min(dp[i - 1][1][0] , dp[i - 1][1][1]) + 1; dp[i][1][0] = dp[i - 1][0][1]; if (vec[i - 1] < vec[i])dp[i][1][0] = min(dp[i][1][0] , dp[i - 1][0][0]); dp[i][1][1] = min(dp[i - 1][0][0] , dp[i - 1][0][1]) + 1; } ans = min({dp[n][0][0] , dp[n][0][1] , dp[n][1][0] , dp[n][1][1]}); cout << ans; /* ll real_ans; out >> real_ans; cout << real_ans << ' ' << ans << endl; if (real_ans != ans) { cout << n << endl; for (int i = 1; i <= n; i++) cout << vec[i] << ' '; cout << endl; cout << ans << ' ' << real_ans; return 0; } in.close(); out.close(); } */ } /* */ |