#include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; typedef vector<ll> vi; const int S = 1e6 + 7; const int mod = 1e9 + 7; ll tab[S], n, m, dp[S][3]; // inject here void solve(){ string s; cin >> n; ll res = mod; for (int o = 0; o < 2; o ++){ // max min no change dp[0][0] = dp[0][1] = dp[0][2] = 0; for (int i = 1; i <= n; i ++){ cin >> tab[i]; bool is_max = i & 1 ^ o; dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + 1; dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + 1; if (is_max) dp[i][1] = mod; else dp[i][0] = mod; dp[i][2] = dp[i - 1][is_max]; bool are_in_order = ( !is_max ? tab[i - 1] < tab[i] : tab[i] < tab[i - 1] ); if (!i || are_in_order) dp[i][2] = min(dp[i][2], dp[i - 1][2]); //cout << "o = " << o << " i = " << i << " [" << dp[i][0] << ", " << dp[i][1] << ", " << dp[i][2] << "]\n"; } for (int i = 0; i < 3; i ++){ res = min(res, dp[n][i]); } } cout << res << "\n"; } int main(){ ios_base::sync_with_stdio(0); int t = 1; // cin >> t; while (t --) solve(); 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 | #include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; typedef vector<ll> vi; const int S = 1e6 + 7; const int mod = 1e9 + 7; ll tab[S], n, m, dp[S][3]; // inject here void solve(){ string s; cin >> n; ll res = mod; for (int o = 0; o < 2; o ++){ // max min no change dp[0][0] = dp[0][1] = dp[0][2] = 0; for (int i = 1; i <= n; i ++){ cin >> tab[i]; bool is_max = i & 1 ^ o; dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + 1; dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + 1; if (is_max) dp[i][1] = mod; else dp[i][0] = mod; dp[i][2] = dp[i - 1][is_max]; bool are_in_order = ( !is_max ? tab[i - 1] < tab[i] : tab[i] < tab[i - 1] ); if (!i || are_in_order) dp[i][2] = min(dp[i][2], dp[i - 1][2]); //cout << "o = " << o << " i = " << i << " [" << dp[i][0] << ", " << dp[i][1] << ", " << dp[i][2] << "]\n"; } for (int i = 0; i < 3; i ++){ res = min(res, dp[n][i]); } } cout << res << "\n"; } int main(){ ios_base::sync_with_stdio(0); int t = 1; // cin >> t; while (t --) solve(); return 0; } |