#include <iostream> using namespace std; int high = 1000000000, low = -1000000000; int tab1[50009], out1=0; int tab2[50009], out2=0; int main() { ios_base::sync_with_stdio(0); int cunt; cin >> cunt; cin >> tab1[0]; tab2[0] = tab1[0]; for (int i = 1; i < cunt; i++) { cin >> tab1[i]; tab2[i] = tab1[i]; if (i % 2 == 1) { if (tab1[i - 1] <= tab1[i]) { tab1[i] = low; out1++; } if (tab2[i - 1] >= tab2[i]) { tab2[i] = high; out2++; } } else { if (tab1[i - 1] >= tab1[i]) { tab1[i] = high; out1++; } if (tab2[i - 1] <= tab2[i]) { tab2[i] = low; out2++; } } } cout << min(out1, out2); }
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 | #include <iostream> using namespace std; int high = 1000000000, low = -1000000000; int tab1[50009], out1=0; int tab2[50009], out2=0; int main() { ios_base::sync_with_stdio(0); int cunt; cin >> cunt; cin >> tab1[0]; tab2[0] = tab1[0]; for (int i = 1; i < cunt; i++) { cin >> tab1[i]; tab2[i] = tab1[i]; if (i % 2 == 1) { if (tab1[i - 1] <= tab1[i]) { tab1[i] = low; out1++; } if (tab2[i - 1] >= tab2[i]) { tab2[i] = high; out2++; } } else { if (tab1[i - 1] >= tab1[i]) { tab1[i] = high; out1++; } if (tab2[i - 1] <= tab2[i]) { tab2[i] = low; out2++; } } } cout << min(out1, out2); } |