#include <iostream> #include <unordered_set> #include <queue> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> #include <assert.h> #include <tuple> using namespace std; using ull = unsigned long long; using ll = long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif #define MAX (1000000000) // #define MAX (20) int tab1[51000]; int tab2[51000]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", &tab1[i]); tab2[i] = tab1[i]; } int changes = 0; // 1st try - FIRST is UP bool increasing = true; for (int i = 0; i < n-1; ++i) { if (increasing) { if (tab1[i+1] > tab1[i]) { // OK, no need to change } else { // change tab[i+1] so it is increasing tab1[i + 1] = MAX; changes++; } } else { if (tab1[i+1] < tab1[i]) { // OK, no need to change } else { // change tab[i+1] so it is decreasing tab1[i + 1] = -MAX; changes++; } } increasing = !increasing; } int changes2 = 0; // 2nd try - FIRST is DOWN increasing = false; for (int i = 0; i < n-1; ++i) { if (increasing) { if (tab2[i+1] > tab2[i]) { // OK, no need to change } else { // change tab[i+1] so it is increasing tab2[i + 1] = MAX; dbg("change2 tab[%d]\n", i + 1); changes2++; } } else { if (tab2[i+1] < tab2[i]) { // OK, no need to change } else { // change tab[i+1] so it is decreasing dbg("change2 tab[%d]\n", i + 1); tab2[i + 1] = -MAX; changes2++; } } increasing = !increasing; } dbg("%d %d\n", changes, changes2); // for (int i = 0; i < n; ++i) // { // dbg("%d ", tab2[i]); // } // dbg("\n"); cout << std::min(changes, changes2) << endl; // dupa; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <unordered_set> #include <queue> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> #include <assert.h> #include <tuple> using namespace std; using ull = unsigned long long; using ll = long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif #define MAX (1000000000) // #define MAX (20) int tab1[51000]; int tab2[51000]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", &tab1[i]); tab2[i] = tab1[i]; } int changes = 0; // 1st try - FIRST is UP bool increasing = true; for (int i = 0; i < n-1; ++i) { if (increasing) { if (tab1[i+1] > tab1[i]) { // OK, no need to change } else { // change tab[i+1] so it is increasing tab1[i + 1] = MAX; changes++; } } else { if (tab1[i+1] < tab1[i]) { // OK, no need to change } else { // change tab[i+1] so it is decreasing tab1[i + 1] = -MAX; changes++; } } increasing = !increasing; } int changes2 = 0; // 2nd try - FIRST is DOWN increasing = false; for (int i = 0; i < n-1; ++i) { if (increasing) { if (tab2[i+1] > tab2[i]) { // OK, no need to change } else { // change tab[i+1] so it is increasing tab2[i + 1] = MAX; dbg("change2 tab[%d]\n", i + 1); changes2++; } } else { if (tab2[i+1] < tab2[i]) { // OK, no need to change } else { // change tab[i+1] so it is decreasing dbg("change2 tab[%d]\n", i + 1); tab2[i + 1] = -MAX; changes2++; } } increasing = !increasing; } dbg("%d %d\n", changes, changes2); // for (int i = 0; i < n; ++i) // { // dbg("%d ", tab2[i]); // } // dbg("\n"); cout << std::min(changes, changes2) << endl; // dupa; return 0; } |