直接计算逆序对
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int countInversions(vector<int>& arr) {
int inversions = 0;
for (int i = 0; i < arr.size(); ++i) {
for (int j = i + 1; j < arr.size(); ++j) {
if (arr[i] > arr[j]) {
++inversions;
}
}
}
return inversions;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int swaps = countInversions(arr);
cout << "Optimal train swapping takes " << swaps << " swaps." << endl;
}
return 0;
}