// #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> using namespace std; #define MIN -1000000 #define MAX 1000000 int main() { FAST int n; cin >> n; int curr, prevA, prevB; cin >> curr; int swapA = 0; int swapB = 0; // A i%0 => > // B i%0 => < prevA = curr; prevB = curr; for (int i = 0; i < n - 1; i++) { // TODO checks for edge cases: -1M +1M => add max to calculation cin >> curr; if (i % 2) { // swapA if (prevA > curr) { prevA = curr; } else { if (prevA == MIN) { swapA += 50000; } swapA++; prevA = MIN; } // swapB if (prevB < curr) { prevB = curr; } else { if (prevB == MAX) { swapB += 50000; } swapB++; prevB = MAX; } } else { // swapA if (prevA < curr) { prevA = curr; } else { if (prevA == MAX) { swapA += 50000; } swapA++; prevA = MAX; } // swapB if (prevB > curr) { prevB = curr; } else { if (prevB == MIN) { swapB += 50000; } swapB++; prevB = MIN; } } } cout << min(swapA, swapB) << endl; }
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 | // #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> using namespace std; #define MIN -1000000 #define MAX 1000000 int main() { FAST int n; cin >> n; int curr, prevA, prevB; cin >> curr; int swapA = 0; int swapB = 0; // A i%0 => > // B i%0 => < prevA = curr; prevB = curr; for (int i = 0; i < n - 1; i++) { // TODO checks for edge cases: -1M +1M => add max to calculation cin >> curr; if (i % 2) { // swapA if (prevA > curr) { prevA = curr; } else { if (prevA == MIN) { swapA += 50000; } swapA++; prevA = MIN; } // swapB if (prevB < curr) { prevB = curr; } else { if (prevB == MAX) { swapB += 50000; } swapB++; prevB = MAX; } } else { // swapA if (prevA < curr) { prevA = curr; } else { if (prevA == MAX) { swapA += 50000; } swapA++; prevA = MAX; } // swapB if (prevB > curr) { prevB = curr; } else { if (prevB == MIN) { swapB += 50000; } swapB++; prevB = MIN; } } } cout << min(swapA, swapB) << endl; } |