#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pii pair <int, int> #define pli pair <ll, int> #define pll pair <ll, ll> #define ppii pair <pii, pii> #define ppli pair <pli, pli> #define ppll pair <pli, pll> using namespace std; const ll MAX_INF = (1ll<<61); struct cup { ll l, a, b; }; template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <class T1, class T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; } ll D[2][800][40][40]; int Inv[41][41]; pll Ans[41]; void testcase_des() { int n; cin >> n; vector <int> A(n+1); int max_inv = 0; for (int i = 1; i <= n; i++) { cin >> A[i]; for (int j = i-1; j > 0; j--) { if (A[j] > A[i]) { max_inv++; } } } /*for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; Inv[i][j] = Inv[i+1][j]; if (A[i] > A[j]) { Inv[i][j]++; } } } for (int i = 1; i <= n; i++) { D[1][0][i][i] = 1; } for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; D[1][0][i][j] = j-i+1; } } cout << 0 << " " << D[1][0][1][n] <<endl; for (int k = 2; k <= n; k++) { int k0 = k % 2, k1 = (k - 1) % 2; for (int inv = 1; inv <= max_inv; inv++) { for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; if (d+1 <= k) { D[k0][inv][i][j] = 0; } else { D[k0][inv][i][j] = D[k0][inv][i][j-1]; if (inv - Inv[i][j] >= 0) { D[k0][inv][i][j] += D[k1][inv-Inv[i][j]][i][j-1]; } } } } } for (int inv = 0; inv <= max_inv; inv++) { if (D[k0][inv][1][n] > 0) { cout << inv << " " << D[k0][inv][1][n] <<endl; break; } } }*/ for (int i = 1; i <= n; i++) { Ans[i] = {MAX_INF, MAX_INF}; } for (ll mask = 0; mask < (1ll<<n); mask++) { vector <int> V; for (int i = 0; i < n; i++) { if (mask & (1ll<<i)) { V.push_back(i+1); } } int inv = 0, s = V.size(); for (int i = 0; i < s; i++) { for (int j = i+1; j < s; j++) { if (A[V[i]] > A[V[j]]) { inv++; } } } if (Ans[s].fi >= inv) { if (Ans[s].fi == inv) { Ans[s].se++; } else { Ans[s] = {inv, 1}; } } //cout << mask << " " << V << " | s = " << s << " inv = " << inv << " ans = " << Ans[s].fi << " " << Ans[s].se <<endl; } for (int i = 1; i <= n; i++) { cout << Ans[i].fi << " " << Ans[i].se <<endl; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T = 1; // cin >> T; for (int t = 0; t < T; t++) { testcase_des(); } 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pii pair <int, int> #define pli pair <ll, int> #define pll pair <ll, ll> #define ppii pair <pii, pii> #define ppli pair <pli, pli> #define ppll pair <pli, pll> using namespace std; const ll MAX_INF = (1ll<<61); struct cup { ll l, a, b; }; template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <class T1, class T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; } ll D[2][800][40][40]; int Inv[41][41]; pll Ans[41]; void testcase_des() { int n; cin >> n; vector <int> A(n+1); int max_inv = 0; for (int i = 1; i <= n; i++) { cin >> A[i]; for (int j = i-1; j > 0; j--) { if (A[j] > A[i]) { max_inv++; } } } /*for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; Inv[i][j] = Inv[i+1][j]; if (A[i] > A[j]) { Inv[i][j]++; } } } for (int i = 1; i <= n; i++) { D[1][0][i][i] = 1; } for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; D[1][0][i][j] = j-i+1; } } cout << 0 << " " << D[1][0][1][n] <<endl; for (int k = 2; k <= n; k++) { int k0 = k % 2, k1 = (k - 1) % 2; for (int inv = 1; inv <= max_inv; inv++) { for (int d = 1; d < n; d++) { for (int i = 1; i+d <= n; i++) { int j = i + d; if (d+1 <= k) { D[k0][inv][i][j] = 0; } else { D[k0][inv][i][j] = D[k0][inv][i][j-1]; if (inv - Inv[i][j] >= 0) { D[k0][inv][i][j] += D[k1][inv-Inv[i][j]][i][j-1]; } } } } } for (int inv = 0; inv <= max_inv; inv++) { if (D[k0][inv][1][n] > 0) { cout << inv << " " << D[k0][inv][1][n] <<endl; break; } } }*/ for (int i = 1; i <= n; i++) { Ans[i] = {MAX_INF, MAX_INF}; } for (ll mask = 0; mask < (1ll<<n); mask++) { vector <int> V; for (int i = 0; i < n; i++) { if (mask & (1ll<<i)) { V.push_back(i+1); } } int inv = 0, s = V.size(); for (int i = 0; i < s; i++) { for (int j = i+1; j < s; j++) { if (A[V[i]] > A[V[j]]) { inv++; } } } if (Ans[s].fi >= inv) { if (Ans[s].fi == inv) { Ans[s].se++; } else { Ans[s] = {inv, 1}; } } //cout << mask << " " << V << " | s = " << s << " inv = " << inv << " ans = " << Ans[s].fi << " " << Ans[s].se <<endl; } for (int i = 1; i <= n; i++) { cout << Ans[i].fi << " " << Ans[i].se <<endl; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T = 1; // cin >> T; for (int t = 0; t < T; t++) { testcase_des(); } return 0; } |