#include <iostream> using namespace std; int maximum_size = 50000; int min_variable = -1000001; int max_variable = 1000001; int Smaller_bigger(int size, int arr[]){ int low = 1, high = 1, org = 0; for (int i = 1; i < size; i++){ int low2 = maximum_size, high2 = maximum_size, org2 = maximum_size; if(i % 2 == 0) { low2 = min(org + 1, high + 1); org2 = high; if (arr[i] < arr[i - 1]) org2 = min(org2, org); } else if (i % 2 == 1){ high2 = min(org + 1, low + 1); org2 = low; if (arr[i] > arr[i - 1]) org2 = min(org2, org); } low = low2; high = high2; org = org2; } return min(low, min(high, org)); } int Bigger_smaller(int size, int arr[]){ int low = 1, high = 1, org = 0; for (int i = 1; i < size; i++){ int low2 = maximum_size, high2 = maximum_size, org2 = maximum_size; if(i % 2 == 1) { low2 = min(org + 1, high + 1); org2 = high; if (arr[i] < arr[i - 1]) org2 = min(org2, org); } else if (i % 2 == 0){ high2 = min(org + 1, low + 1); org2 = low; if (arr[i] > arr[i - 1]) org2 = min(org2, org); } low = low2; high = high2; org = org2; } return min(low, min(high, org)); } int main() { int n; int arr[maximum_size]; cin >> n; for (int i = 0; i < n; i++){ cin >> arr[i]; } int first_check = Smaller_bigger(n, arr); int second_check = Bigger_smaller(n, arr); int result = (first_check < second_check) ? first_check : second_check; cout << result << 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 56 57 58 59 60 61 62 | #include <iostream> using namespace std; int maximum_size = 50000; int min_variable = -1000001; int max_variable = 1000001; int Smaller_bigger(int size, int arr[]){ int low = 1, high = 1, org = 0; for (int i = 1; i < size; i++){ int low2 = maximum_size, high2 = maximum_size, org2 = maximum_size; if(i % 2 == 0) { low2 = min(org + 1, high + 1); org2 = high; if (arr[i] < arr[i - 1]) org2 = min(org2, org); } else if (i % 2 == 1){ high2 = min(org + 1, low + 1); org2 = low; if (arr[i] > arr[i - 1]) org2 = min(org2, org); } low = low2; high = high2; org = org2; } return min(low, min(high, org)); } int Bigger_smaller(int size, int arr[]){ int low = 1, high = 1, org = 0; for (int i = 1; i < size; i++){ int low2 = maximum_size, high2 = maximum_size, org2 = maximum_size; if(i % 2 == 1) { low2 = min(org + 1, high + 1); org2 = high; if (arr[i] < arr[i - 1]) org2 = min(org2, org); } else if (i % 2 == 0){ high2 = min(org + 1, low + 1); org2 = low; if (arr[i] > arr[i - 1]) org2 = min(org2, org); } low = low2; high = high2; org = org2; } return min(low, min(high, org)); } int main() { int n; int arr[maximum_size]; cin >> n; for (int i = 0; i < n; i++){ cin >> arr[i]; } int first_check = Smaller_bigger(n, arr); int second_check = Bigger_smaller(n, arr); int result = (first_check < second_check) ? first_check : second_check; cout << result << endl; return 0; } |