#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; typedef pair <int,LL> il; #define pb push_back const int INF = 2147483647; const int N = 41; int i, j, k, n, l, tab[N], best[N], cou[N], act, bat[N], ind; int main() { scanf("%d", &n); for (i=1;i<=n;i++) { scanf("%d", &tab[i]); best[i] = INF; } for (j=1;j<(1<<n);j++) { ind = 0; for (k=0;k<n;k++) if ((1<<k) & j) bat[ind++] = tab[k + 1]; act = 0; for (k=0;k<ind;k++) for (l=k+1;l<ind;l++) if (bat[k] > bat[l]) act++; if (act < best[ind]) { best[ind] = act; cou[ind] = 1; } else if (act == best[ind]) cou[ind]++; } for (i=1;i<=n;i++) printf("%d %d\n", best[i], cou[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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; typedef pair <int,LL> il; #define pb push_back const int INF = 2147483647; const int N = 41; int i, j, k, n, l, tab[N], best[N], cou[N], act, bat[N], ind; int main() { scanf("%d", &n); for (i=1;i<=n;i++) { scanf("%d", &tab[i]); best[i] = INF; } for (j=1;j<(1<<n);j++) { ind = 0; for (k=0;k<n;k++) if ((1<<k) & j) bat[ind++] = tab[k + 1]; act = 0; for (k=0;k<ind;k++) for (l=k+1;l<ind;l++) if (bat[k] > bat[l]) act++; if (act < best[ind]) { best[ind] = act; cou[ind] = 1; } else if (act == best[ind]) cou[ind]++; } for (i=1;i<=n;i++) printf("%d %d\n", best[i], cou[i]); return 0; } |