#include<bits/stdc++.h> using namespace std; #define nd second #define st first #define pb push_back typedef long long ll; typedef long double ld; const int N = 50 * 1000 + 7; int dp[2][2][N]; int tab[N]; void Solve() { int n, w; cin >> n; for(int i = 1; i <= n; ++i) cin >> tab[i]; dp[1][0][1] = 0; dp[1][1][1] = 1; dp[0][0][1] = 0; dp[0][1][1] = 1; for(int i = 2; i <= n; ++i) { dp[0][0][i] = dp[1][1][i - 1]; if(tab[i] < tab[i - 1]) dp[0][0][i] = min(dp[1][0][i - 1], dp[1][0][i - 1]); dp[0][1][i] = min(dp[1][0][i - 1], dp[1][1][i - 1]) + 1; dp[1][0][i] = dp[0][1][i - 1]; if(tab[i] > tab[i - 1]) dp[1][0][i] = min(dp[0][0][i - 1], dp[0][0][i - 1]); dp[1][1][i] = min(dp[0][0][i - 1], dp[0][1][i - 1]) + 1; //cout << dp[0][0][i] << " " << dp[0][1][i] << " " << dp[1][0][i] << " " << dp[1][1][i] << "\n"; } w = min(min(dp[0][1][n], dp[0][0][n]), min(dp[1][1][n], dp[1][0][n])); cout << w << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); 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 | #include<bits/stdc++.h> using namespace std; #define nd second #define st first #define pb push_back typedef long long ll; typedef long double ld; const int N = 50 * 1000 + 7; int dp[2][2][N]; int tab[N]; void Solve() { int n, w; cin >> n; for(int i = 1; i <= n; ++i) cin >> tab[i]; dp[1][0][1] = 0; dp[1][1][1] = 1; dp[0][0][1] = 0; dp[0][1][1] = 1; for(int i = 2; i <= n; ++i) { dp[0][0][i] = dp[1][1][i - 1]; if(tab[i] < tab[i - 1]) dp[0][0][i] = min(dp[1][0][i - 1], dp[1][0][i - 1]); dp[0][1][i] = min(dp[1][0][i - 1], dp[1][1][i - 1]) + 1; dp[1][0][i] = dp[0][1][i - 1]; if(tab[i] > tab[i - 1]) dp[1][0][i] = min(dp[0][0][i - 1], dp[0][0][i - 1]); dp[1][1][i] = min(dp[0][0][i - 1], dp[0][1][i - 1]) + 1; //cout << dp[0][0][i] << " " << dp[0][1][i] << " " << dp[1][0][i] << " " << dp[1][1][i] << "\n"; } w = min(min(dp[0][1][n], dp[0][0][n]), min(dp[1][1][n], dp[1][0][n])); cout << w << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); Solve(); return 0; } |