#pragma GCC optimize("Ofast") #include <iostream> #include <vector> #include <climits> //INT_MAX #define UP 0 #define DOWN 1 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int n; std::cin >> n; int* arr = new int[n]; for(int i = 0; i < n; i++) { std::cin >> arr[i]; } //4 scenarions //1. increasing, first unchanged //2. increasing, first decreased //3. decreasing, first unchanged //4. decreasing, first increased //output minimal value out of them int answ = INT_MAX; #pragma GCC unroll 2 for(int d = UP; d <= DOWN; d++) { int temp_answ = 0; bool next = d; bool prev_changed = false; for(int i = 1; i < n; i++) { if(next == UP) { if(arr[i - 1] >= arr[i] && prev_changed == false) { temp_answ++; prev_changed = true; } else prev_changed = false; } else if(arr[i - 1] <= arr[i] && prev_changed == false) { prev_changed = true; temp_answ++; } else prev_changed = false; next = !next; } if(temp_answ < answ) answ = temp_answ; //now the same but we assume weve changed the first one temp_answ = 1; next = d; prev_changed = true; for(int i = 1; i < n; i++) { if(next == UP) { if(arr[i - 1] >= arr[i] && prev_changed == false) { temp_answ++; prev_changed = true; } else prev_changed = false; } else if(arr[i - 1] <= arr[i] && prev_changed == false) { prev_changed = true; temp_answ++; } else prev_changed = false; next = !next; } if(temp_answ < answ) answ = temp_answ; } std::cout << answ << '\n'; delete arr; }
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #pragma GCC optimize("Ofast") #include <iostream> #include <vector> #include <climits> //INT_MAX #define UP 0 #define DOWN 1 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int n; std::cin >> n; int* arr = new int[n]; for(int i = 0; i < n; i++) { std::cin >> arr[i]; } //4 scenarions //1. increasing, first unchanged //2. increasing, first decreased //3. decreasing, first unchanged //4. decreasing, first increased //output minimal value out of them int answ = INT_MAX; #pragma GCC unroll 2 for(int d = UP; d <= DOWN; d++) { int temp_answ = 0; bool next = d; bool prev_changed = false; for(int i = 1; i < n; i++) { if(next == UP) { if(arr[i - 1] >= arr[i] && prev_changed == false) { temp_answ++; prev_changed = true; } else prev_changed = false; } else if(arr[i - 1] <= arr[i] && prev_changed == false) { prev_changed = true; temp_answ++; } else prev_changed = false; next = !next; } if(temp_answ < answ) answ = temp_answ; //now the same but we assume weve changed the first one temp_answ = 1; next = d; prev_changed = true; for(int i = 1; i < n; i++) { if(next == UP) { if(arr[i - 1] >= arr[i] && prev_changed == false) { temp_answ++; prev_changed = true; } else prev_changed = false; } else if(arr[i - 1] <= arr[i] && prev_changed == false) { prev_changed = true; temp_answ++; } else prev_changed = false; next = !next; } if(temp_answ < answ) answ = temp_answ; } std::cout << answ << '\n'; delete arr; } |