#include <iostream> #include <vector> int main() { long count{}; std::vector<long> numbers; std::cin >> count; for (long i = 0; i < count; i++) { long tmp{}; std::cin >> tmp; numbers.push_back(tmp); } long countVersion1{}; long countVersion2{}; std::vector<bool> version1Changes(numbers.size(), false); std::vector<bool> version2Changes(numbers.size(), false); for (long i = 1; i < numbers.size(); i++) { if (i % 2 == 0) { if (numbers[i] > numbers[i - 1]) { countVersion1++; // for debugging } else if (!version1Changes[i - 1]) { version1Changes[i] = true; } if (numbers[i] < numbers[i - 1]) { countVersion2++; // for debugging } else if (!version2Changes[i - 1]) { version2Changes[i] = true; } } else { if (numbers[i] < numbers[i - 1]) { countVersion1++; // for debugging } else if (!version1Changes[i - 1]) { version1Changes[i] = true; } if (numbers[i] > numbers[i - 1]) { countVersion2++; // for debugging } else if (!version2Changes[i - 1]) { version2Changes[i] = true; } } } long countChanges1{}; long countChanges2{}; for (auto c : version1Changes) { if (c) { countChanges1++; } } for (auto c : version2Changes) { if (c) { countChanges2++; } } std::cout << std::min(countChanges1, countChanges2) << 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 55 56 57 58 | #include <iostream> #include <vector> int main() { long count{}; std::vector<long> numbers; std::cin >> count; for (long i = 0; i < count; i++) { long tmp{}; std::cin >> tmp; numbers.push_back(tmp); } long countVersion1{}; long countVersion2{}; std::vector<bool> version1Changes(numbers.size(), false); std::vector<bool> version2Changes(numbers.size(), false); for (long i = 1; i < numbers.size(); i++) { if (i % 2 == 0) { if (numbers[i] > numbers[i - 1]) { countVersion1++; // for debugging } else if (!version1Changes[i - 1]) { version1Changes[i] = true; } if (numbers[i] < numbers[i - 1]) { countVersion2++; // for debugging } else if (!version2Changes[i - 1]) { version2Changes[i] = true; } } else { if (numbers[i] < numbers[i - 1]) { countVersion1++; // for debugging } else if (!version1Changes[i - 1]) { version1Changes[i] = true; } if (numbers[i] > numbers[i - 1]) { countVersion2++; // for debugging } else if (!version2Changes[i - 1]) { version2Changes[i] = true; } } } long countChanges1{}; long countChanges2{}; for (auto c : version1Changes) { if (c) { countChanges1++; } } for (auto c : version2Changes) { if (c) { countChanges2++; } } std::cout << std::min(countChanges1, countChanges2) << std::endl; return 0; } |