#include <bits/stdc++.h> using namespace std; constexpr int nax = 5e4 + 1, inf = 1e9; int a[nax][2], res[nax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; a[0][0] = -inf; a[0][1] = inf; for (int i = 1; i <= n; ++i) { int x; cin >> x; if (x > a[i - 1][0]) { a[i][1] = x; res[i][1] = res[i - 1][0]; } else { a[i][1] = inf; res[i][1] = res[i - 1][0] + 1; } if (x < a[i - 1][1]) { a[i][0] = x; res[i][0] = res[i - 1][1]; } else { a[i][0] = -inf; res[i][0] = res[i - 1][1] + 1; } } cout << min(res[n][0], res[n][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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; constexpr int nax = 5e4 + 1, inf = 1e9; int a[nax][2], res[nax][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; a[0][0] = -inf; a[0][1] = inf; for (int i = 1; i <= n; ++i) { int x; cin >> x; if (x > a[i - 1][0]) { a[i][1] = x; res[i][1] = res[i - 1][0]; } else { a[i][1] = inf; res[i][1] = res[i - 1][0] + 1; } if (x < a[i - 1][1]) { a[i][0] = x; res[i][0] = res[i - 1][1]; } else { a[i][0] = -inf; res[i][0] = res[i - 1][1] + 1; } } cout << min(res[n][0], res[n][1]) << "\n"; } |