fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. int countInversions(const vector<int>& a) {
  7. int inv = 0;
  8. int n = a.size();
  9. for (int i = 0; i < n; ++i)
  10. for (int j = i+1; j < n; ++j)
  11. if (a[i] > a[j]) ++inv;
  12. return inv;
  13. }
  14.  
  15. void solve() {
  16. int n;
  17. cin >> n;
  18. vector<int> p(n);
  19. for (auto &x : p) cin >> x;
  20.  
  21. int minInv = LLONG_MAX;
  22.  
  23. for (int mask = 0; mask < (1 << n); ++mask) {
  24. vector<int> a(n);
  25. for (int i = 0; i < n; ++i) {
  26. if ((mask >> i) & 1)
  27. a[i] = 2 * n - p[i];
  28. else
  29. a[i] = p[i];
  30. }
  31. int inv = countInversions(a);
  32. minInv = min(minInv, inv);
  33. }
  34.  
  35. cout << minInv << '\n';
  36. }
  37.  
  38. int32_t main() {
  39. ios::sync_with_stdio(false);
  40. cin.tie(nullptr);
  41.  
  42. int t;
  43. cin >> t;
  44. while (t--) solve();
  45. }
  46.  
Success #stdin #stdout 0s 5320KB
stdin
5
2
2 1
3
2 1 3
4
4 3 2 1
5
2 3 1 5 4
6
2 3 4 1 5 6
stdout
0
1
0
2
2