#include<cstdio> #include<vector> using namespace std; int changesNum(vector<int>& inversions) { int changesToMake = 0; for (int i = 0; i < inversions.size(); i++) { if (inversions[i] == 2) { changesToMake++; inversions[i - 1]--; inversions[i] = 0; inversions[i + 1]--; } } for (int i = 0; i < inversions.size(); i++) { if (inversions[i] == 1) { changesToMake++; if (i < inversions.size()) { inversions[i + 1]--; } } } return changesToMake; } int main() { int recordLength; scanf("%d\n", &recordLength); vector<int> lInversions(recordLength, 0); vector<int> gInversions(recordLength, 0); int last; scanf("%d", &last); for (int i = 1; i < recordLength; i++) { int current; scanf("%d", ¤t); if ((current > last && i % 2 == 1) || (current < last && i % 2 == 0) || (current == last)) { lInversions[i - 1]++; lInversions[i]++; } if ((current > last && i % 2 == 0) || (current < last && i % 2 == 1) || (current == last)) { gInversions[i - 1]++; gInversions[i]++; } last = current; } int lChanges = changesNum(lInversions); int gChanges = changesNum(gInversions); if (lChanges <= gChanges) { printf("%d\n", lChanges); } else { printf("%d\n", gChanges); } }
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 | #include<cstdio> #include<vector> using namespace std; int changesNum(vector<int>& inversions) { int changesToMake = 0; for (int i = 0; i < inversions.size(); i++) { if (inversions[i] == 2) { changesToMake++; inversions[i - 1]--; inversions[i] = 0; inversions[i + 1]--; } } for (int i = 0; i < inversions.size(); i++) { if (inversions[i] == 1) { changesToMake++; if (i < inversions.size()) { inversions[i + 1]--; } } } return changesToMake; } int main() { int recordLength; scanf("%d\n", &recordLength); vector<int> lInversions(recordLength, 0); vector<int> gInversions(recordLength, 0); int last; scanf("%d", &last); for (int i = 1; i < recordLength; i++) { int current; scanf("%d", ¤t); if ((current > last && i % 2 == 1) || (current < last && i % 2 == 0) || (current == last)) { lInversions[i - 1]++; lInversions[i]++; } if ((current > last && i % 2 == 0) || (current < last && i % 2 == 1) || (current == last)) { gInversions[i - 1]++; gInversions[i]++; } last = current; } int lChanges = changesNum(lInversions); int gChanges = changesNum(gInversions); if (lChanges <= gChanges) { printf("%d\n", lChanges); } else { printf("%d\n", gChanges); } } |