#include <iostream> using namespace std; enum class Direction { HEIGHER, LOWER }; int changes(int record[], int n, Direction direction) { int result = 0, i = 1; while (i < n) { if (direction == Direction::HEIGHER) { if (record[i] <= record[i-1]) { result++; i += 2; } else { direction = Direction::LOWER; i++; } } else { if (record[i] >= record[i-1]) { result++; i += 2; } else { direction = Direction::HEIGHER; i++; } } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int record[n]; for (int i = 0; i < n; i++) cin >> record[i]; int changes1 = changes(record, n, Direction::HEIGHER); int changes2 = changes(record, n, Direction::LOWER); cout << (changes1 < changes2 ? changes1 : changes2) << "\n"; 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 | #include <iostream> using namespace std; enum class Direction { HEIGHER, LOWER }; int changes(int record[], int n, Direction direction) { int result = 0, i = 1; while (i < n) { if (direction == Direction::HEIGHER) { if (record[i] <= record[i-1]) { result++; i += 2; } else { direction = Direction::LOWER; i++; } } else { if (record[i] >= record[i-1]) { result++; i += 2; } else { direction = Direction::HEIGHER; i++; } } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int record[n]; for (int i = 0; i < n; i++) cin >> record[i]; int changes1 = changes(record, n, Direction::HEIGHER); int changes2 = changes(record, n, Direction::LOWER); cout << (changes1 < changes2 ? changes1 : changes2) << "\n"; return 0; } |