#include <iostream> #include <ctime> #include <vector> using namespace std; /** 5 4 1 3 3 1 ANS: 1 */ int main() { clock_t start = clock(); int n, k; cin >> n; std::vector<int> arr; for(int i = 0; i < n; i++) { cin >> k; arr.push_back(k); } bool nextSmaller = true; int changes = 0; for(int i = 0; i < n - 1; i++) { if((arr[i] > arr[i + 1] && nextSmaller) || (arr[i] < arr[i + 1] && !nextSmaller)) { nextSmaller = !nextSmaller; } else { changes++; i++; } } bool nextSmaller2 = false; int changes2 = 0; for(int i = 0; i < n - 1; i++) { if((arr[i] > arr[i + 1] && nextSmaller2) || (arr[i] < arr[i + 1] && !nextSmaller2)) { nextSmaller2 = !nextSmaller2; } else { changes2++; i++; } } cout << min(changes, changes2) << 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 | #include <iostream> #include <ctime> #include <vector> using namespace std; /** 5 4 1 3 3 1 ANS: 1 */ int main() { clock_t start = clock(); int n, k; cin >> n; std::vector<int> arr; for(int i = 0; i < n; i++) { cin >> k; arr.push_back(k); } bool nextSmaller = true; int changes = 0; for(int i = 0; i < n - 1; i++) { if((arr[i] > arr[i + 1] && nextSmaller) || (arr[i] < arr[i + 1] && !nextSmaller)) { nextSmaller = !nextSmaller; } else { changes++; i++; } } bool nextSmaller2 = false; int changes2 = 0; for(int i = 0; i < n - 1; i++) { if((arr[i] > arr[i + 1] && nextSmaller2) || (arr[i] < arr[i + 1] && !nextSmaller2)) { nextSmaller2 = !nextSmaller2; } else { changes2++; i++; } } cout << min(changes, changes2) << endl; return 0; } |