#include <iostream> #include <limits.h> using namespace std; enum : char { AMBIGUOUS, INCREASING, DECREASING } next_step; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, answer = 0; cin >> n; int arr[n]; for(auto& i : arr) cin >> i; if(arr[0] == arr[1]) { if(arr[1] < arr[2]) arr[0] = INT32_MAX; else if(arr[1] > arr[2]) arr[0] = INT32_MIN; } for(int* i = arr+1; i < arr+n; ++i) { if(*i == i[-1]) { if(next_step == AMBIGUOUS) ++i; next_step = AMBIGUOUS; ++answer; } else if(next_step == INCREASING && i[-1] > *i) { ++answer; *i = INT32_MAX; } else if(next_step == DECREASING && i[-1] < *i) { ++answer; *i = INT32_MIN; } next_step = i[-1] == *i ? AMBIGUOUS : i[-1] < *i ? DECREASING : INCREASING; } cout << answer; }
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 | #include <iostream> #include <limits.h> using namespace std; enum : char { AMBIGUOUS, INCREASING, DECREASING } next_step; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, answer = 0; cin >> n; int arr[n]; for(auto& i : arr) cin >> i; if(arr[0] == arr[1]) { if(arr[1] < arr[2]) arr[0] = INT32_MAX; else if(arr[1] > arr[2]) arr[0] = INT32_MIN; } for(int* i = arr+1; i < arr+n; ++i) { if(*i == i[-1]) { if(next_step == AMBIGUOUS) ++i; next_step = AMBIGUOUS; ++answer; } else if(next_step == INCREASING && i[-1] > *i) { ++answer; *i = INT32_MAX; } else if(next_step == DECREASING && i[-1] < *i) { ++answer; *i = INT32_MIN; } next_step = i[-1] == *i ? AMBIGUOUS : i[-1] < *i ? DECREASING : INCREASING; } cout << answer; } |