#include <bits/stdc++.h> using namespace std; int main() { size_t n; cin >> n; vector<int> A(n); for(size_t i = 0; i < n; i++) cin >> A[i]; vector<pair<int, int>> result(n+1, {INT_MAX, 0}); for(size_t mask = 1; mask < (1u << n); mask++) { auto k = __builtin_popcount(mask); size_t occ = 0; int inv = 0; for(size_t i = 0; i < n; i++) if(mask >> i & 1) { inv += __builtin_popcount(occ >> A[i]); occ |= 1 << A[i]; } if(inv < result[k].first) result[k] = {inv, 0}; if(inv == result[k].first) result[k].second++; } for(size_t k = 1; k <= n; k++) cout << result[k].first << " " << result[k].second << endl; }
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 | #include <bits/stdc++.h> using namespace std; int main() { size_t n; cin >> n; vector<int> A(n); for(size_t i = 0; i < n; i++) cin >> A[i]; vector<pair<int, int>> result(n+1, {INT_MAX, 0}); for(size_t mask = 1; mask < (1u << n); mask++) { auto k = __builtin_popcount(mask); size_t occ = 0; int inv = 0; for(size_t i = 0; i < n; i++) if(mask >> i & 1) { inv += __builtin_popcount(occ >> A[i]); occ |= 1 << A[i]; } if(inv < result[k].first) result[k] = {inv, 0}; if(inv == result[k].first) result[k].second++; } for(size_t k = 1; k <= n; k++) cout << result[k].first << " " << result[k].second << endl; } |