#include <bits/stdc++.h> using namespace std; int MAX = 1000000000; int MIN = -1000000000; int main() { int size; cin >> size; int sounds[50001]; int sounds2[50001]; for (int i = 0; i < size; i++) { cin >> sounds[i]; sounds2[i] = sounds[i]; } bool change = false; int changes1 = 0; int changes2 = 0; for (int i = 1; i < size; i++) { if (sounds[i] < sounds[i - 1] && !change) { sounds[i] = MAX; changes1++; } else if (sounds[i] > sounds[i - 1] && change) { sounds[i] = MIN; changes1++; } else if (sounds[i] == sounds[i - 1]) { if (!change) { sounds[i] = MAX; } else { sounds[i] = MIN; } changes1++; } if (sounds2[i] > sounds2[i - 1] && !change) { sounds2[i] = MIN; changes2++; } else if (sounds2[i] < sounds2[i - 1] && change) { sounds2[i] = MAX; changes2++; } else if (sounds2[i] == sounds2[i - 1]) { if (!change) { sounds2[i] = MIN; } else { sounds2[i] = MAX; } changes2++; } change = !change; } cout << min(changes1, changes2) << endl; 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 | #include <bits/stdc++.h> using namespace std; int MAX = 1000000000; int MIN = -1000000000; int main() { int size; cin >> size; int sounds[50001]; int sounds2[50001]; for (int i = 0; i < size; i++) { cin >> sounds[i]; sounds2[i] = sounds[i]; } bool change = false; int changes1 = 0; int changes2 = 0; for (int i = 1; i < size; i++) { if (sounds[i] < sounds[i - 1] && !change) { sounds[i] = MAX; changes1++; } else if (sounds[i] > sounds[i - 1] && change) { sounds[i] = MIN; changes1++; } else if (sounds[i] == sounds[i - 1]) { if (!change) { sounds[i] = MAX; } else { sounds[i] = MIN; } changes1++; } if (sounds2[i] > sounds2[i - 1] && !change) { sounds2[i] = MIN; changes2++; } else if (sounds2[i] < sounds2[i - 1] && change) { sounds2[i] = MAX; changes2++; } else if (sounds2[i] == sounds2[i - 1]) { if (!change) { sounds2[i] = MIN; } else { sounds2[i] = MAX; } changes2++; } change = !change; } cout << min(changes1, changes2) << endl; return 0; } |