#include <vector> #include <iostream> int best(const std::vector<int> &ints, bool firstLower) { bool nextLower = firstLower; int left = ints[0]; const int INFINITY = 10000000; int changes = 0; for (int i = 0; i < ints.size() - 1; i++) { if (left >= ints[i+1] && nextLower) { left = INFINITY; changes += 1; } else if (left <= ints[i+1] && !nextLower) { left = -INFINITY; changes += 1; } else { left = ints[i+1]; } nextLower = !nextLower; } return changes; } int main() { int nints; scanf("%d", &nints); std::vector<int> ints(nints); for (int i = 0; i < nints; i++) { scanf("%d", &ints[i]); } int a = best(ints, false); int b = best(ints, true); printf("%d\n", std::min(a, b)); 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 | #include <vector> #include <iostream> int best(const std::vector<int> &ints, bool firstLower) { bool nextLower = firstLower; int left = ints[0]; const int INFINITY = 10000000; int changes = 0; for (int i = 0; i < ints.size() - 1; i++) { if (left >= ints[i+1] && nextLower) { left = INFINITY; changes += 1; } else if (left <= ints[i+1] && !nextLower) { left = -INFINITY; changes += 1; } else { left = ints[i+1]; } nextLower = !nextLower; } return changes; } int main() { int nints; scanf("%d", &nints); std::vector<int> ints(nints); for (int i = 0; i < nints; i++) { scanf("%d", &ints[i]); } int a = best(ints, false); int b = best(ints, true); printf("%d\n", std::min(a, b)); return 0; } |