#include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcount __popcnt #else #define popcount __builtin_popcount #endif int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a(n); for (auto&& v : a) { std::cin >> v; v--; } unsigned edge_count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[i] < a[j]) edge_count++; } } auto bitsize = (edge_count + 31) >> 5; std::vector<std::vector<unsigned>> g(n, std::vector<unsigned>(bitsize)); edge_count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[i] < a[j]) { g[i][edge_count >> 5] |= 1u << (edge_count & 31); g[j][edge_count >> 5] |= 1u << (edge_count & 31); edge_count++; } } } std::vector<unsigned> bor(bitsize); std::vector<unsigned> bxor(bitsize); std::vector<std::pair<int, long long>> r(n, std::make_pair(n * n, 0)); for (unsigned long long i = 1; i < 1uLL << n; i++) { std::fill(bor.begin(), bor.end(), 0); std::fill(bxor.begin(), bxor.end(), ~0u); auto it = g.begin(); int size = -1; for (unsigned long long j = i; j; j >>= 1, ++it) { if (j & 1) { size++; std::transform(bor.begin(), bor.end(), it->begin(), bor.begin(), std::bit_or<unsigned> {}); std::transform(bxor.begin(), bxor.end(), it->begin(), bxor.begin(), std::bit_xor<unsigned> {}); } } std::transform(bor.begin(), bor.end(), bxor.begin(), bor.begin(), std::bit_and<unsigned> {}); int val = std::accumulate(bor.begin(), bor.end(), 0, [](int lhs, unsigned rhs) { return lhs + popcount(rhs); }); auto& [min_val, count] = r[size]; if (val < min_val) { min_val = val; count = 1; } else if (val == min_val) { count++; } } for (const auto& [min_val, count] : r) { std::cout << min_val << ' ' << count << '\n'; } 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 | #include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcount __popcnt #else #define popcount __builtin_popcount #endif int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a(n); for (auto&& v : a) { std::cin >> v; v--; } unsigned edge_count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[i] < a[j]) edge_count++; } } auto bitsize = (edge_count + 31) >> 5; std::vector<std::vector<unsigned>> g(n, std::vector<unsigned>(bitsize)); edge_count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[i] < a[j]) { g[i][edge_count >> 5] |= 1u << (edge_count & 31); g[j][edge_count >> 5] |= 1u << (edge_count & 31); edge_count++; } } } std::vector<unsigned> bor(bitsize); std::vector<unsigned> bxor(bitsize); std::vector<std::pair<int, long long>> r(n, std::make_pair(n * n, 0)); for (unsigned long long i = 1; i < 1uLL << n; i++) { std::fill(bor.begin(), bor.end(), 0); std::fill(bxor.begin(), bxor.end(), ~0u); auto it = g.begin(); int size = -1; for (unsigned long long j = i; j; j >>= 1, ++it) { if (j & 1) { size++; std::transform(bor.begin(), bor.end(), it->begin(), bor.begin(), std::bit_or<unsigned> {}); std::transform(bxor.begin(), bxor.end(), it->begin(), bxor.begin(), std::bit_xor<unsigned> {}); } } std::transform(bor.begin(), bor.end(), bxor.begin(), bor.begin(), std::bit_and<unsigned> {}); int val = std::accumulate(bor.begin(), bor.end(), 0, [](int lhs, unsigned rhs) { return lhs + popcount(rhs); }); auto& [min_val, count] = r[size]; if (val < min_val) { min_val = val; count = 1; } else if (val == min_val) { count++; } } for (const auto& [min_val, count] : r) { std::cout << min_val << ' ' << count << '\n'; } return 0; } |