#include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int INF = 1000000000LL; int a[40], mi[40]; LL wa[40]; int main() { INT(n); REP(i,n) scanf("%d", &a[i]); REP(i,n) mi[i] = INF; int n2 = 1 << n; FOR(m,1,n2) { int k = __builtin_popcount(m) - 1; int inv = 0; REP(i,m) if (m & (1 << i)) REP(j,i) if (m & (1 << j)) inv += a[j] > a[i]; if (inv < mi[k]) { mi[k] = inv; wa[k] = 1; } else if (inv == mi[k]) ++wa[k]; } REP(k,n) printf("%d %lld\n", mi[k], wa[k]); }
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 | #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int INF = 1000000000LL; int a[40], mi[40]; LL wa[40]; int main() { INT(n); REP(i,n) scanf("%d", &a[i]); REP(i,n) mi[i] = INF; int n2 = 1 << n; FOR(m,1,n2) { int k = __builtin_popcount(m) - 1; int inv = 0; REP(i,m) if (m & (1 << i)) REP(j,i) if (m & (1 << j)) inv += a[j] > a[i]; if (inv < mi[k]) { mi[k] = inv; wa[k] = 1; } else if (inv == mi[k]) ++wa[k]; } REP(k,n) printf("%d %lld\n", mi[k], wa[k]); } |