#include<bits/stdc++.h> using namespace std; const int MAXN = 1e5; int dp[MAXN][2]; int solve(const vector<int>& t) { int n = t.size(); dp[0][0] = 0; dp[0][1] = 1; for(int i=1;i<n;i++) { int add = ((i%2 == 0 && t[i] < t[i-1]) || (i%2 == 1 && t[i] > t[i-1]) ? 0 : 1); dp[i][0] = min(dp[i-1][1], dp[i-1][0] + add); dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1; } return min(dp[n-1][0], dp[n-1][1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> t(n); for(int& i : t) cin >> i; int result = solve(t); for(int& i : t) i = -i; result = min(result, solve(t)); cout << result << "\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 | #include<bits/stdc++.h> using namespace std; const int MAXN = 1e5; int dp[MAXN][2]; int solve(const vector<int>& t) { int n = t.size(); dp[0][0] = 0; dp[0][1] = 1; for(int i=1;i<n;i++) { int add = ((i%2 == 0 && t[i] < t[i-1]) || (i%2 == 1 && t[i] > t[i-1]) ? 0 : 1); dp[i][0] = min(dp[i-1][1], dp[i-1][0] + add); dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1; } return min(dp[n-1][0], dp[n-1][1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> t(n); for(int& i : t) cin >> i; int result = solve(t); for(int& i : t) i = -i; result = min(result, solve(t)); cout << result << "\n"; return 0; } |