#include <iostream> #include <vector> using namespace std; typedef vector<int> vi; typedef long long int ll; int bitc(ll x) { int res = 0; while (x) { if (x & 1) res++; x >>= 1; } return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; const ll mask = 1L << n; vi a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<ll>> res(n+1, vector<ll>(n*(n+1) / 2, 0)); for (ll i = 1; i < mask; i++) { int b = bitc(i); int inv = 0; for (int j = 0; j < n; j++) { if (!(i & (1L << j))) continue; for (int k = j + 1; k < n; k++) { if ((i & (1L << k)) && a[j] > a[k]) inv++; } } res[b][inv]++; } for (int k = 1; k <= n; k++) { for (uint i = 0; i < res[k].size(); i++) { if (res[k][i] > 0) { cout << i << ' ' << res[k][i] << endl; break; } } } return 0; }
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 | #include <iostream> #include <vector> using namespace std; typedef vector<int> vi; typedef long long int ll; int bitc(ll x) { int res = 0; while (x) { if (x & 1) res++; x >>= 1; } return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; const ll mask = 1L << n; vi a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<ll>> res(n+1, vector<ll>(n*(n+1) / 2, 0)); for (ll i = 1; i < mask; i++) { int b = bitc(i); int inv = 0; for (int j = 0; j < n; j++) { if (!(i & (1L << j))) continue; for (int k = j + 1; k < n; k++) { if ((i & (1L << k)) && a[j] > a[k]) inv++; } } res[b][inv]++; } for (int k = 1; k <= n; k++) { for (uint i = 0; i < res[k].size(); i++) { if (res[k][i] > 0) { cout << i << ' ' << res[k][i] << endl; break; } } } return 0; } |