#include <iostream> using namespace std; int main() { int changes = 0, changes2 = 0, n, a; cin >> n; int tab[n]; int tab2[n]; for (int i = 0; i < n; i++){ cin >> a; tab[i] = a; tab2[i] = a; } bool nextSmaller = false; if (tab[0] < tab[1]) nextSmaller = true; else if (tab[0] == tab[1]) { tab[1] = -1000000000; changes++; } for (int i = 1; i < n - 1; i++) { if (nextSmaller && tab[i] <= tab[i + 1]) { tab[i + 1] = -1000000000; changes++; } else if (!nextSmaller && tab[i] >= tab[i + 1]) { tab[i + 1] = 1000000000; changes++; } nextSmaller = !nextSmaller; } nextSmaller = false; if (tab2[n-2] > tab2[n-1]) nextSmaller = true; else if (tab2[n-2] == tab2[n-1]) { tab2[n-1] = -1000000000; changes2++; } for (int i = n-1; i >= 1; i--) { if (nextSmaller && tab2[i - 1] <= tab2[i]) { tab2[i - 1] = 1000000000; changes2++; } else if (!nextSmaller && tab2[i - 1] >= tab2[i]) { tab2[i + 1] = -1000000000; changes2++; } nextSmaller = !nextSmaller; } cout<<min(changes,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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> using namespace std; int main() { int changes = 0, changes2 = 0, n, a; cin >> n; int tab[n]; int tab2[n]; for (int i = 0; i < n; i++){ cin >> a; tab[i] = a; tab2[i] = a; } bool nextSmaller = false; if (tab[0] < tab[1]) nextSmaller = true; else if (tab[0] == tab[1]) { tab[1] = -1000000000; changes++; } for (int i = 1; i < n - 1; i++) { if (nextSmaller && tab[i] <= tab[i + 1]) { tab[i + 1] = -1000000000; changes++; } else if (!nextSmaller && tab[i] >= tab[i + 1]) { tab[i + 1] = 1000000000; changes++; } nextSmaller = !nextSmaller; } nextSmaller = false; if (tab2[n-2] > tab2[n-1]) nextSmaller = true; else if (tab2[n-2] == tab2[n-1]) { tab2[n-1] = -1000000000; changes2++; } for (int i = n-1; i >= 1; i--) { if (nextSmaller && tab2[i - 1] <= tab2[i]) { tab2[i - 1] = 1000000000; changes2++; } else if (!nextSmaller && tab2[i - 1] >= tab2[i]) { tab2[i + 1] = -1000000000; changes2++; } nextSmaller = !nextSmaller; } cout<<min(changes,changes2); return 0; } |