#include <bits/stdc++.h> using namespace std; int n, wynik; int t[100000]; int dp[100000][4]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; } dp[1][1] = 1; dp[1][3] = 1; for (int i = 2; i <= n; i++) { dp[i][0] = dp[i - 1][3]; dp[i][1] = min(dp[i - 1][2], dp[i - 1][3]) + 1; dp[i][2] = dp[i - 1][1]; dp[i][3] = min(dp[i - 1][0], dp[i - 1][1]) + 1; if (t[i] > t[i - 1]) dp[i][2] = min(dp[i][2], dp[i - 1][0]); if (t[i] < t[i - 1]) dp[i][0] = min(dp[i][0], dp[i - 1][2]); } wynik = min(min(dp[n][0], dp[n][1]), min(dp[n][2], dp[n][3])); cout << wynik << endl; }
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 | #include <bits/stdc++.h> using namespace std; int n, wynik; int t[100000]; int dp[100000][4]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; } dp[1][1] = 1; dp[1][3] = 1; for (int i = 2; i <= n; i++) { dp[i][0] = dp[i - 1][3]; dp[i][1] = min(dp[i - 1][2], dp[i - 1][3]) + 1; dp[i][2] = dp[i - 1][1]; dp[i][3] = min(dp[i - 1][0], dp[i - 1][1]) + 1; if (t[i] > t[i - 1]) dp[i][2] = min(dp[i][2], dp[i - 1][0]); if (t[i] < t[i - 1]) dp[i][0] = min(dp[i][0], dp[i - 1][2]); } wynik = min(min(dp[n][0], dp[n][1]), min(dp[n][2], dp[n][3])); cout << wynik << endl; } |