//GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 50 * 1000 + 10, INF = 1e9; int n; int a[nax]; int dp[nax][3][3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i : {0,1}) dp[1][0][i] = dp[1][1][i] = i; for (int i = 2; i <= n; ++i) { dp[i][0][0] = dp[i - 1][1][1]; if (a[i] > a[i - 1]) dp[i][0][0] = min(dp[i][0][0], dp[i - 1][1][0]); dp[i][0][1] = min(dp[i-1][1][0], dp[i-1][1][1]) + 1; dp[i][1][0] = dp[i-1][0][1]; if (a[i] < a[i - 1]) dp[i][1][0] = min(dp[i][1][0], dp[i-1][0][0]); dp[i][1][1] = min(dp[i-1][0][0], dp[i-1][0][1]) + 1; } int ans = INF; for (int i : {0, 1}) { for (int j : {0, 1}) { ans = min(ans, dp[n][i][j]); } } cout << ans; }
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 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 50 * 1000 + 10, INF = 1e9; int n; int a[nax]; int dp[nax][3][3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i : {0,1}) dp[1][0][i] = dp[1][1][i] = i; for (int i = 2; i <= n; ++i) { dp[i][0][0] = dp[i - 1][1][1]; if (a[i] > a[i - 1]) dp[i][0][0] = min(dp[i][0][0], dp[i - 1][1][0]); dp[i][0][1] = min(dp[i-1][1][0], dp[i-1][1][1]) + 1; dp[i][1][0] = dp[i-1][0][1]; if (a[i] < a[i - 1]) dp[i][1][0] = min(dp[i][1][0], dp[i-1][0][0]); dp[i][1][1] = min(dp[i-1][0][0], dp[i-1][0][1]) + 1; } int ans = INF; for (int i : {0, 1}) { for (int j : {0, 1}) { ans = min(ans, dp[n][i][j]); } } cout << ans; } |