HDU4911-Inversion, inversion
Q: Exchange adjacent two elements k times according to the requirements of the question, so that the last number of reverse orders is the least.
Idea: if the number of reverse orders is greater than 0, there is 0 <= I <n to enable Ai switching. After Ai + 1, the number of reverse orders is reduced by 1, and the answer is max (inversion-k, 0 );
Use merge sort to calculate the reverse logarithm.
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 1000005;int arr[MAXN], b[MAXN];int n, k;long long cnt;void merge_sort(int * a, int x, int y, int *b) { if (y - x > 1) { int m = x + (y - x) / 2; int p = x, q = m, i = x; merge_sort(a, x, m, b); merge_sort(a, m, y, b); while (p < m || q < y) { if (q >= y || (p < m && a[p] <= a[q])) b[i++] = a[p++]; else { b[i++] = a[q++]; cnt += m - p; } } for (i = x; i < y; i++) a[i] = b[i]; }}int main() { while (scanf("%d%d", &n, &k) != EOF) { for (int i = 0; i < n; i++) scanf("%d", &arr[i]); cnt = 0; merge_sort(arr, 0, n, b); if (cnt - k > 0) cnt -= k; else cnt = 0; cout << cnt << endl; } return 0;}