#include <iostream> #include <vector> #define UP true #define DOWN false std::string temp_str; int read_number(bool is_last) { std::getline(std::cin, temp_str, (is_last) ? '\n' : ' '); return std::stoi(temp_str); } int count_changes(const std::vector<int>& sounds, bool next_direction) { int changes_count = 0; bool skip = false; for (int i = 0; i < sounds.size(); ++i) { if (i == 0) { continue; } if (skip) { skip = false; next_direction = !next_direction; continue; } if (next_direction == UP and sounds[i - 1] >= sounds[i] or next_direction == DOWN and sounds[i - 1] <= sounds[i]) { changes_count++; skip = true; } next_direction = !next_direction; } return changes_count; } int main() { int sounds_count; std::cin >> sounds_count; std::vector<int> sounds; sounds.reserve(sounds_count); for (int i = 0; i < sounds_count; ++i) { sounds.push_back(read_number(i == sounds_count - 1)); } std::cout << std::min(count_changes(sounds, UP), count_changes(sounds, DOWN)); 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 55 56 57 | #include <iostream> #include <vector> #define UP true #define DOWN false std::string temp_str; int read_number(bool is_last) { std::getline(std::cin, temp_str, (is_last) ? '\n' : ' '); return std::stoi(temp_str); } int count_changes(const std::vector<int>& sounds, bool next_direction) { int changes_count = 0; bool skip = false; for (int i = 0; i < sounds.size(); ++i) { if (i == 0) { continue; } if (skip) { skip = false; next_direction = !next_direction; continue; } if (next_direction == UP and sounds[i - 1] >= sounds[i] or next_direction == DOWN and sounds[i - 1] <= sounds[i]) { changes_count++; skip = true; } next_direction = !next_direction; } return changes_count; } int main() { int sounds_count; std::cin >> sounds_count; std::vector<int> sounds; sounds.reserve(sounds_count); for (int i = 0; i < sounds_count; ++i) { sounds.push_back(read_number(i == sounds_count - 1)); } std::cout << std::min(count_changes(sounds, UP), count_changes(sounds, DOWN)); return 0; } |