#include<bits/stdc++.h> using namespace std; #define rep(a, b) for(int a = 0; a < (b); ++a) const int LIM=5e4+7; int dp[LIM][3][2], T[LIM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n+1) dp[i][1][1]=dp[i][2][0]=LIM; for(int i=1; i<=n; ++i) { cin >> T[i]; dp[i][1][0]=min(dp[i-1][0][1], dp[i-1][2][1])+1; dp[i][2][1]=min(dp[i-1][0][0], dp[i-1][1][0])+1; dp[i][0][0]=dp[i-1][2][1]; if(i==1 || T[i]<T[i-1]) dp[i][0][0]=min(dp[i][0][0], dp[i-1][0][1]); dp[i][0][1]=dp[i-1][1][0]; if(i==1 || T[i]>T[i-1]) dp[i][0][1]=min(dp[i][0][1], dp[i-1][0][0]); } int ans=LIM; rep(i, 3) rep(j, 2) ans=min(ans, dp[n][i][j]); cout << ans << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<bits/stdc++.h> using namespace std; #define rep(a, b) for(int a = 0; a < (b); ++a) const int LIM=5e4+7; int dp[LIM][3][2], T[LIM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n+1) dp[i][1][1]=dp[i][2][0]=LIM; for(int i=1; i<=n; ++i) { cin >> T[i]; dp[i][1][0]=min(dp[i-1][0][1], dp[i-1][2][1])+1; dp[i][2][1]=min(dp[i-1][0][0], dp[i-1][1][0])+1; dp[i][0][0]=dp[i-1][2][1]; if(i==1 || T[i]<T[i-1]) dp[i][0][0]=min(dp[i][0][0], dp[i-1][0][1]); dp[i][0][1]=dp[i-1][1][0]; if(i==1 || T[i]>T[i-1]) dp[i][0][1]=min(dp[i][0][1], dp[i-1][0][0]); } int ans=LIM; rep(i, 3) rep(j, 2) ans=min(ans, dp[n][i][j]); cout << ans << '\n'; } |