#include <iostream> using namespace std; int main() { int n, t; cin >> n; cin >> t; bool k = 0; int ost = t; int a = 0; bool p = 1; int ost2 = t; int b = 0; for(int i = 2; i <= n; ++i) { cin >> t; if(!k) { if(t >= ost) { ++a; ost = -1e9; } else ost = t; } else { if(t <= ost) { ++a; ost = 1e9; } else ost = t; } k ^= 1; if(!p) { if(t >= ost2) { ++b; ost2 = -1e9; } else ost2 = t; } else { if(t <= ost2) { ++b; ost2 = 1e9; } else ost2 = t; } p ^= 1; } cout << min(a, b); 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 63 64 65 66 67 68 69 | #include <iostream> using namespace std; int main() { int n, t; cin >> n; cin >> t; bool k = 0; int ost = t; int a = 0; bool p = 1; int ost2 = t; int b = 0; for(int i = 2; i <= n; ++i) { cin >> t; if(!k) { if(t >= ost) { ++a; ost = -1e9; } else ost = t; } else { if(t <= ost) { ++a; ost = 1e9; } else ost = t; } k ^= 1; if(!p) { if(t >= ost2) { ++b; ost2 = -1e9; } else ost2 = t; } else { if(t <= ost2) { ++b; ost2 = 1e9; } else ost2 = t; } p ^= 1; } cout << min(a, b); return 0; } |