#include <iostream> #include <vector> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define int long long using namespace std; const int MAX_N = 5e4; int n, record[MAX_N], res; string s; short compare(int a, int b) { if (a > b) return 1; if (a == b) return 0; return -1; } void read() { boost; cin >> n; for (int i = 0; i < n; i++) { cin >> record[i]; } } int findMinimalChanges(const int diffs[], const int otherDiffs[]) { int minimalChanges = 0; for (int i = 0; i < n - 1; i++) { if (diffs[i] != otherDiffs[i]) { minimalChanges++; if (diffs[i + 1] != otherDiffs[i + 1]) { i++; } } } return minimalChanges; } int32_t main() { read(); int diffs[n - 1], diffs1[n - 1], diffs2[n - 1]; diffs[0] = compare(record[1], record[0]); diffs1[0] = 1; diffs2[0] = -1; for (int i = 1; i < n - 1; i++) { diffs[i] = compare(record[i + 1], record[i]); diffs1[i] = -diffs1[i - 1]; diffs2[i] = -diffs2[i - 1]; } int res1 = findMinimalChanges(diffs, diffs1); int res2 = findMinimalChanges(diffs, diffs2); res = min(res1, res2); cout << res << 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 | #include <iostream> #include <vector> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define int long long using namespace std; const int MAX_N = 5e4; int n, record[MAX_N], res; string s; short compare(int a, int b) { if (a > b) return 1; if (a == b) return 0; return -1; } void read() { boost; cin >> n; for (int i = 0; i < n; i++) { cin >> record[i]; } } int findMinimalChanges(const int diffs[], const int otherDiffs[]) { int minimalChanges = 0; for (int i = 0; i < n - 1; i++) { if (diffs[i] != otherDiffs[i]) { minimalChanges++; if (diffs[i + 1] != otherDiffs[i + 1]) { i++; } } } return minimalChanges; } int32_t main() { read(); int diffs[n - 1], diffs1[n - 1], diffs2[n - 1]; diffs[0] = compare(record[1], record[0]); diffs1[0] = 1; diffs2[0] = -1; for (int i = 1; i < n - 1; i++) { diffs[i] = compare(record[i + 1], record[i]); diffs1[i] = -diffs1[i - 1]; diffs2[i] = -diffs2[i - 1]; } int res1 = findMinimalChanges(diffs, diffs1); int res2 = findMinimalChanges(diffs, diffs2); res = min(res1, res2); cout << res << endl; } |