#include <iostream> const int maxN = 5e5; int t[maxN]; int up[maxN][2]; int down[maxN][2]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n; std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> t[i]; up[0][1] = 1; down[0][1] = 1; for (int i = 1; i < n; ++i) { up[i][1] = down[i - 1][0] + 1; up[i][0] = down[i - 1][1]; if (t[i] > t[i - 1]) up[i][0] = down[i - 1][0]; down[i][1] = up[i - 1][0] + 1; down[i][0] = up[i - 1][1]; if (t[i] < t[i - 1]) down[i][0] = up[i - 1][0]; } std::cout << std::min(up[n - 1][0], down[n - 1][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 | #include <iostream> const int maxN = 5e5; int t[maxN]; int up[maxN][2]; int down[maxN][2]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n; std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> t[i]; up[0][1] = 1; down[0][1] = 1; for (int i = 1; i < n; ++i) { up[i][1] = down[i - 1][0] + 1; up[i][0] = down[i - 1][1]; if (t[i] > t[i - 1]) up[i][0] = down[i - 1][0]; down[i][1] = up[i - 1][0] + 1; down[i][0] = up[i - 1][1]; if (t[i] < t[i - 1]) down[i][0] = up[i - 1][0]; } std::cout << std::min(up[n - 1][0], down[n - 1][0]); } |