#include <bits/stdc++.h> int n; int tab[1'000'000]; int tabA[1'000'000]; int tabB[1'000'000]; constexpr int MIN = -1'000'000'000; constexpr int MAX = 1'000'000'000; int A(int arr[]) { int output = 0; for (int i = 1; i < n; ++i) { if (i % 2) { if (arr[i] >= arr[i - 1]) { output += 1; arr[i] = MIN; } } else { if (arr[i] <= arr[i - 1]) { output += 1; arr[i] = MAX; } } } return output; } int B(int arr[]) { int output = 0; for (int i = 1; i < n; ++i) { if (i % 2){ if (arr[i] <= arr[i - 1]){ output += 1; arr[i] = MAX; } } else if (arr[i] >= arr[i - 1]){ output += 1; arr[i] = MIN; } } return output; } int main() { std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> tab[i]; std::copy(tab, tab + n, tabA); std::copy(tab, tab + n, tabB); if (n == 1) std::cout << 0; else std::cout << std::min(A(tabA), B(tabB)); 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 | #include <bits/stdc++.h> int n; int tab[1'000'000]; int tabA[1'000'000]; int tabB[1'000'000]; constexpr int MIN = -1'000'000'000; constexpr int MAX = 1'000'000'000; int A(int arr[]) { int output = 0; for (int i = 1; i < n; ++i) { if (i % 2) { if (arr[i] >= arr[i - 1]) { output += 1; arr[i] = MIN; } } else { if (arr[i] <= arr[i - 1]) { output += 1; arr[i] = MAX; } } } return output; } int B(int arr[]) { int output = 0; for (int i = 1; i < n; ++i) { if (i % 2){ if (arr[i] <= arr[i - 1]){ output += 1; arr[i] = MAX; } } else if (arr[i] >= arr[i - 1]){ output += 1; arr[i] = MIN; } } return output; } int main() { std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> tab[i]; std::copy(tab, tab + n, tabA); std::copy(tab, tab + n, tabB); if (n == 1) std::cout << 0; else std::cout << std::min(A(tabA), B(tabB)); return 0; } |