HDU 4911: Inversion
Posted zjnu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4911: Inversion相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, INF = INT_MAX;
int n, k, A[N], L[N], R[N];
long long cnt;
void Merge(int p, int q, int r) {
int n1 = q - p + 1, n2 = r - q;
for (int i = 0; i < n1; i++)
L[i] = A[i + p];
for (int i = 0; i < n2; i++)
R[i] = A[i + q + 1];
L[n1] = INF;
R[n2] = INF;
for (int i = 0, j = 0, k = p; k <= r; k++)
if (L[i] <= R[j])
A[k] = L[i++];
else {
A[k] = R[j++];
cnt += n1 - i;
}
}
void MergeSort(int p, int r) {
if (p < r) {
int q = (p + r) / 2;
MergeSort(p, q);
MergeSort(q + 1, r);
Merge(p, q, r);
}
}
int main() {
ios::sync_with_stdio(false);
while (cin >> n >> k) {
cnt = 0;
for (int i = 0; i < n; i++)
cin >> A[i];
MergeSort(0, n - 1);
cout << max(0LL, cnt - k) << endl;
}
return 0;
}
以上是关于HDU 4911: Inversion的主要内容,如果未能解决你的问题,请参考以下文章