#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; int dp[2] = {0, 0}; int prev[2] = {(int)-1e9, (int)1e9}; bool inc[2] = {true, false}; for (int i = 1, x; i <= n; i++, swap(inc[0], inc[1])) { cin >> x; for (int j = 0; j < 2; j++) { if (inc[j] && prev[j] >= x) { dp[j]++; prev[j] = 1e9; } else if (!inc[j] && prev[j] <= x) { dp[j]++; prev[j] = -1e9; } else { prev[j] = x; } } } cout << min(dp[0], dp[1]) << '\n'; }
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 | #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; int dp[2] = {0, 0}; int prev[2] = {(int)-1e9, (int)1e9}; bool inc[2] = {true, false}; for (int i = 1, x; i <= n; i++, swap(inc[0], inc[1])) { cin >> x; for (int j = 0; j < 2; j++) { if (inc[j] && prev[j] >= x) { dp[j]++; prev[j] = 1e9; } else if (!inc[j] && prev[j] <= x) { dp[j]++; prev[j] = -1e9; } else { prev[j] = x; } } } cout << min(dp[0], dp[1]) << '\n'; } |