#include <iostream> #include <vector> #include <array> int sign(int a, int b) { return b > a ? 1 : (b < a ? -1 : 0); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> data(n); for (int i = 0; i < n; ++i) { std::cin >> data[i]; } int total_rising = 0; bool rising = true; for (int i = 0; i < n - 1; ++i) { int first = sign(data[i], data[i+1]); if (first != 1 && rising || first != -1 && !rising) { ++total_rising; ++i; continue; } rising = !rising; } int total_falling = 0; bool falling = true; for (int i = 0; i < n - 1; ++i) { int first = sign(data[i], data[i+1]); if (first != -1 && falling || first != 1 && !falling) { ++total_falling; ++i; continue; } falling = !falling; } //std::cout << total_rising << " " << total_falling << std::endl; std::cout << std::min(total_falling, total_rising) << std::endl; return 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> #include <array> int sign(int a, int b) { return b > a ? 1 : (b < a ? -1 : 0); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> data(n); for (int i = 0; i < n; ++i) { std::cin >> data[i]; } int total_rising = 0; bool rising = true; for (int i = 0; i < n - 1; ++i) { int first = sign(data[i], data[i+1]); if (first != 1 && rising || first != -1 && !rising) { ++total_rising; ++i; continue; } rising = !rising; } int total_falling = 0; bool falling = true; for (int i = 0; i < n - 1; ++i) { int first = sign(data[i], data[i+1]); if (first != -1 && falling || first != 1 && !falling) { ++total_falling; ++i; continue; } falling = !falling; } //std::cout << total_rising << " " << total_falling << std::endl; std::cout << std::min(total_falling, total_rising) << std::endl; return 0; } |