#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int changes1 = 0; int changes2 = 0; for (int i = 0; i < n - 1; ++i) { int change = a[i] - a[i + 1]; if (i%2==1) { if (change <= 0) { changes1++; i++; } } else if (change >= 0) { changes1++; i++; } } for (int i = 0; i < n - 1; ++i) { int change = a[i] - a[i + 1]; if (i%2==0) { if (change <= 0) { changes2++; i++; } } else if (change >= 0) { changes2++; i++; } } cout << min(changes1,changes2); 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 | #include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int changes1 = 0; int changes2 = 0; for (int i = 0; i < n - 1; ++i) { int change = a[i] - a[i + 1]; if (i%2==1) { if (change <= 0) { changes1++; i++; } } else if (change >= 0) { changes1++; i++; } } for (int i = 0; i < n - 1; ++i) { int change = a[i] - a[i + 1]; if (i%2==0) { if (change <= 0) { changes2++; i++; } } else if (change >= 0) { changes2++; i++; } } cout << min(changes1,changes2); return 0; } |