#include <bits/stdc++.h> #define ll long long #define mk std::make_pair #define st first #define nd second const int MAX_N = 40; int n; std::pair<int, ll> res[MAX_N+3]; int t[MAX_N+3]; void input(){ std::cin >> n; for (int i = 0; i < n; i++) std::cin >> t[i]; } bool is_bit(int mask, int pos){ return (mask & (1<<pos)) != 0; } int get_inv(std::vector<int> &v){ int res = 0; for (int i = 0; i < v.size(); i++) for (int j = i+1; j < v.size(); j++) if (v[j] < v[i]) res++; return res; } void solve(){ for (int i = 1; i <= n; i++) res[i] = mk(INT_MAX, 0); std::vector<int> v; for (int mask = 1; mask < (1<<n); mask++){ v.clear(); for (int j = 0; j < n; j++) if (is_bit(mask, j)) v.push_back(t[j]); int inv = get_inv(v); int size = v.size(); if (inv < res[size].st) res[size] = mk(inv, 1); else if (inv == res[size].st) res[size].nd++; } for (int i = 1; i <= n; i++) std::cout << res[i].st << " " << res[i].nd << "\n"; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); solve(); }
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 48 49 50 51 52 53 54 55 | #include <bits/stdc++.h> #define ll long long #define mk std::make_pair #define st first #define nd second const int MAX_N = 40; int n; std::pair<int, ll> res[MAX_N+3]; int t[MAX_N+3]; void input(){ std::cin >> n; for (int i = 0; i < n; i++) std::cin >> t[i]; } bool is_bit(int mask, int pos){ return (mask & (1<<pos)) != 0; } int get_inv(std::vector<int> &v){ int res = 0; for (int i = 0; i < v.size(); i++) for (int j = i+1; j < v.size(); j++) if (v[j] < v[i]) res++; return res; } void solve(){ for (int i = 1; i <= n; i++) res[i] = mk(INT_MAX, 0); std::vector<int> v; for (int mask = 1; mask < (1<<n); mask++){ v.clear(); for (int j = 0; j < n; j++) if (is_bit(mask, j)) v.push_back(t[j]); int inv = get_inv(v); int size = v.size(); if (inv < res[size].st) res[size] = mk(inv, 1); else if (inv == res[size].st) res[size].nd++; } for (int i = 1; i <= n; i++) std::cout << res[i].st << " " << res[i].nd << "\n"; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); solve(); } |