#include <iostream> #include <set> #include <map> #include <algorithm> using namespace std; enum expectation { higher, lower }; expectation neg(expectation expectation) { return expectation == lower ? higher : lower; } unsigned int cnt(expectation first_expectation, vector<int> nums) { unsigned idx = 1; expectation curr = neg(first_expectation); unsigned cnt = 0; while (idx < nums.size()) { if (curr == lower && nums[idx] >= nums[idx - 1]) { nums[idx] = -10000000; cnt += 1; } if (curr == higher && nums[idx] <= nums[idx - 1]) { nums[idx] = 10000000; cnt += 1; } curr = neg(curr); idx += 1; } return cnt; } int main() { ios_base::sync_with_stdio(false); int a, n; vector<int> nums; cin >> n; while (n--) { cin >> a; nums.push_back(a); } unsigned first_low = cnt(lower, nums); unsigned first_high = cnt(higher, nums); cout << min(first_low, first_high) << 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 | #include <iostream> #include <set> #include <map> #include <algorithm> using namespace std; enum expectation { higher, lower }; expectation neg(expectation expectation) { return expectation == lower ? higher : lower; } unsigned int cnt(expectation first_expectation, vector<int> nums) { unsigned idx = 1; expectation curr = neg(first_expectation); unsigned cnt = 0; while (idx < nums.size()) { if (curr == lower && nums[idx] >= nums[idx - 1]) { nums[idx] = -10000000; cnt += 1; } if (curr == higher && nums[idx] <= nums[idx - 1]) { nums[idx] = 10000000; cnt += 1; } curr = neg(curr); idx += 1; } return cnt; } int main() { ios_base::sync_with_stdio(false); int a, n; vector<int> nums; cin >> n; while (n--) { cin >> a; nums.push_back(a); } unsigned first_low = cnt(lower, nums); unsigned first_high = cnt(higher, nums); cout << min(first_low, first_high) << endl; return 0; } |