#include <iostream> #include <string.h> using namespace std; int ornitolog(long long int *arr, int n) { int doo[n] = {0}, dont[n] = {0}; doo[0] = 1; bool increase = true; for(int i = 1; i < n; ++i) { doo[i] = min(doo[i - 1], dont[i - 1]) + 1; dont[i] = min(doo[i - 1], ((arr[i - 1] < arr[i] && increase) || (arr[i - 1] > arr[i] && !increase)) ? dont[i - 1] : doo[i]); increase = !increase; } int res = dont[n - 1]; for(int i = 0; i < n; ++i) { doo[i] = 0; dont[i] = 0; } doo[0] = 1; increase = false; for(int i = 1; i < n; ++i) { doo[i] = min(doo[i - 1], dont[i - 1]) + 1; dont[i] = min(doo[i - 1], ((arr[i - 1] < arr[i] && increase) || (arr[i - 1] > arr[i] && !increase)) ? dont[i - 1] : doo[i]); increase = !increase; } return min(res, dont[n - 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; long long int arr[n]; for(int i = 0; i < n; ++i) cin >> arr[i]; printf("%d", ornitolog(arr, 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 | #include <iostream> #include <string.h> using namespace std; int ornitolog(long long int *arr, int n) { int doo[n] = {0}, dont[n] = {0}; doo[0] = 1; bool increase = true; for(int i = 1; i < n; ++i) { doo[i] = min(doo[i - 1], dont[i - 1]) + 1; dont[i] = min(doo[i - 1], ((arr[i - 1] < arr[i] && increase) || (arr[i - 1] > arr[i] && !increase)) ? dont[i - 1] : doo[i]); increase = !increase; } int res = dont[n - 1]; for(int i = 0; i < n; ++i) { doo[i] = 0; dont[i] = 0; } doo[0] = 1; increase = false; for(int i = 1; i < n; ++i) { doo[i] = min(doo[i - 1], dont[i - 1]) + 1; dont[i] = min(doo[i - 1], ((arr[i - 1] < arr[i] && increase) || (arr[i - 1] > arr[i] && !increase)) ? dont[i - 1] : doo[i]); increase = !increase; } return min(res, dont[n - 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; long long int arr[n]; for(int i = 0; i < n; ++i) cin >> arr[i]; printf("%d", ornitolog(arr, n)); return 0; } |