#include <cstdio> #define pc __builtin_popcount int a[40]; int w[40]; int ans_v[40]; int ans_c[40]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); a[i]--; w[a[i]] = i; } for (int i = 1; i <= n; i++) ans_v[i] = 696969696; int *inv = new int[1 << n]; int *mem = new int[1 << n]; inv[0] = 0; mem[0] = 0; int l = 0; for (int m = 1; m < (1 << n); m++) { int m2 = m ^ (1 << l); mem[m] = mem[m2] | (1 << w[l]); inv[m] = inv[m2] + pc(mem[m2] & (1 + ~(1 << w[l]))); int k = pc(m); if (ans_v[k] == inv[m]) { ans_c[k]++; } else if (ans_v[k] > inv[m]) { ans_c[k] = 1; ans_v[k] = inv[m]; } if ((m & (m + 1)) == 0) l++; } for (int i = 1; i <= n; i++) printf("%d %d\n", ans_v[i], ans_c[i]); }
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 | #include <cstdio> #define pc __builtin_popcount int a[40]; int w[40]; int ans_v[40]; int ans_c[40]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); a[i]--; w[a[i]] = i; } for (int i = 1; i <= n; i++) ans_v[i] = 696969696; int *inv = new int[1 << n]; int *mem = new int[1 << n]; inv[0] = 0; mem[0] = 0; int l = 0; for (int m = 1; m < (1 << n); m++) { int m2 = m ^ (1 << l); mem[m] = mem[m2] | (1 << w[l]); inv[m] = inv[m2] + pc(mem[m2] & (1 + ~(1 << w[l]))); int k = pc(m); if (ans_v[k] == inv[m]) { ans_c[k]++; } else if (ans_v[k] > inv[m]) { ans_c[k] = 1; ans_v[k] = inv[m]; } if ((m & (m + 1)) == 0) l++; } for (int i = 1; i <= n; i++) printf("%d %d\n", ans_v[i], ans_c[i]); } |