#include<bits/stdc++.h> using namespace std; int n, a[50004], ans; pair <int, int> dp[50004]; void f(int k) { dp[0].second = 1; for(int i = 1; i < n; i ++){ if(i % 2 == k){ if(a[i - 1] < a[i]) dp[i].first = min(dp[i - 1].first, dp[i - 1].second); else dp[i].first = dp[i - 1].second; } else { if(a[i] < a[i - 1]) dp[i].first = min(dp[i - 1].first, dp[i - 1].second); else dp[i].first = dp[i - 1].second; } dp[i].second = min(dp[i - 1].first, dp[i - 1].second) + 1; } } int main() { cin >> n; for(int i = 0; i < n; i ++){ cin >> a[i]; } f(0); ans = min(dp[n - 1].first, dp[n - 1].second); f(1); ans = min(ans,min(dp[n - 1].first, dp[n - 1].second)); cout<< ans << "\n"; 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 | #include<bits/stdc++.h> using namespace std; int n, a[50004], ans; pair <int, int> dp[50004]; void f(int k) { dp[0].second = 1; for(int i = 1; i < n; i ++){ if(i % 2 == k){ if(a[i - 1] < a[i]) dp[i].first = min(dp[i - 1].first, dp[i - 1].second); else dp[i].first = dp[i - 1].second; } else { if(a[i] < a[i - 1]) dp[i].first = min(dp[i - 1].first, dp[i - 1].second); else dp[i].first = dp[i - 1].second; } dp[i].second = min(dp[i - 1].first, dp[i - 1].second) + 1; } } int main() { cin >> n; for(int i = 0; i < n; i ++){ cin >> a[i]; } f(0); ans = min(dp[n - 1].first, dp[n - 1].second); f(1); ans = min(ans,min(dp[n - 1].first, dp[n - 1].second)); cout<< ans << "\n"; return 0; } |