#include <vector> #include <cstdio> #include <set> #include <algorithm> #include <utility> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a(n); if (n > 30) { puts("PODDAJEMY"); return 0; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i]--; } vector<int> ret(n+1, n*n), cnt(n+1); for (int mask = 0; mask < (1<<n); mask++) { int x = 0; int kt = 0; for (int i = 0; i < n; i++) if (mask & (1<<i)) { x += __builtin_popcount(kt & ((1<<n) - (1<<a[i]))); kt += 1<<a[i]; } int in = __builtin_popcount(mask); if (ret[in] > x) { ret[in] = x; cnt[in] = 0; } if (ret[in] == x) { cnt[in]++; } } for (int i = 1; i <= n; i++) printf("%d %d\n",ret[i],cnt[i]); 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 | #include <vector> #include <cstdio> #include <set> #include <algorithm> #include <utility> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a(n); if (n > 30) { puts("PODDAJEMY"); return 0; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i]--; } vector<int> ret(n+1, n*n), cnt(n+1); for (int mask = 0; mask < (1<<n); mask++) { int x = 0; int kt = 0; for (int i = 0; i < n; i++) if (mask & (1<<i)) { x += __builtin_popcount(kt & ((1<<n) - (1<<a[i]))); kt += 1<<a[i]; } int in = __builtin_popcount(mask); if (ret[in] > x) { ret[in] = x; cnt[in] = 0; } if (ret[in] == x) { cnt[in]++; } } for (int i = 1; i <= n; i++) printf("%d %d\n",ret[i],cnt[i]); return 0; } |