#include <iostream> using namespace std; int main(){ int n, cnt_a = 0, cnt_b = 0; // a - pierwszy rosnacy, b - pierwszy malejacy cin >> n; bool a_changed = false, b_changed = false; int v1, v2; cin >> v1; for(int i = 1; i < n; i++){ cin >> v2; if(i % 2 == 0){ if(v2 - v1 >= 0 && !a_changed){ a_changed = true; cnt_a++; } else a_changed = false; if(v2 - v1 <= 0 && !b_changed){ b_changed = true; cnt_b++; } else b_changed = false; } else{ if(v2 - v1 >= 0 && !b_changed){ b_changed = true; cnt_b++; } else b_changed = false; if(v2 - v1 <= 0 && !a_changed){ a_changed = true; cnt_a++; } else a_changed = false; } v1 = v2; } cout << min(cnt_a, cnt_b) << endl; }
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 | #include <iostream> using namespace std; int main(){ int n, cnt_a = 0, cnt_b = 0; // a - pierwszy rosnacy, b - pierwszy malejacy cin >> n; bool a_changed = false, b_changed = false; int v1, v2; cin >> v1; for(int i = 1; i < n; i++){ cin >> v2; if(i % 2 == 0){ if(v2 - v1 >= 0 && !a_changed){ a_changed = true; cnt_a++; } else a_changed = false; if(v2 - v1 <= 0 && !b_changed){ b_changed = true; cnt_b++; } else b_changed = false; } else{ if(v2 - v1 >= 0 && !b_changed){ b_changed = true; cnt_b++; } else b_changed = false; if(v2 - v1 <= 0 && !a_changed){ a_changed = true; cnt_a++; } else a_changed = false; } v1 = v2; } cout << min(cnt_a, cnt_b) << endl; } |