#include <iostream> #include <algorithm> using namespace std; int n, A[1000000], cop[1000000], ans1 = 0, ans2 = 0; bool b; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; copy(begin(A), end(A), begin(cop)); b = false; for (int i = 0; i < n - 1; i++) { if (b && cop[i + 1] <= cop[i]) { ans1++; cop[i+1] = 1000000; } else if (!b && cop[i + 1] >= cop[i]) { ans1++; cop[i+1] = -1000000; } b = !b; } b = true; for (int i = 0; i < n - 1; i++) { if (b && A[i + 1] <= A[i]) { ans2++; A[i+1] = 1000000; } else if (!b && A[i + 1] >= A[i]) { ans2++; A[i+1] = -1000000; } b = !b; } cout << min(ans1,ans2); }
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 | #include <iostream> #include <algorithm> using namespace std; int n, A[1000000], cop[1000000], ans1 = 0, ans2 = 0; bool b; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; copy(begin(A), end(A), begin(cop)); b = false; for (int i = 0; i < n - 1; i++) { if (b && cop[i + 1] <= cop[i]) { ans1++; cop[i+1] = 1000000; } else if (!b && cop[i + 1] >= cop[i]) { ans1++; cop[i+1] = -1000000; } b = !b; } b = true; for (int i = 0; i < n - 1; i++) { if (b && A[i + 1] <= A[i]) { ans2++; A[i+1] = 1000000; } else if (!b && A[i + 1] >= A[i]) { ans2++; A[i+1] = -1000000; } b = !b; } cout << min(ans1,ans2); } |