#include <bits/stdc++.h> using namespace std; const int MAXN = 45; typedef long long i64; int input[MAXN]; int chosen[MAXN]; int c_size = 0; int inversions = 0; int result[MAXN]; i64 ways[MAXN]; int n; void backtrack(int d) { if (d == n) { return; } int new_inversions = 0; for (int i = 0; i < c_size; i++) { if (chosen[i] > input[d]) { new_inversions++; } } inversions += new_inversions; chosen[c_size] = input[d]; c_size++; if (result[c_size] > inversions) { result[c_size] = inversions; ways[c_size] = 0; } if (result[c_size] == inversions) { ways[c_size]++; } backtrack(d + 1); inversions -= new_inversions; c_size--; backtrack(d + 1); } void read() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &input[i]); } for (int i = 1; i <= n; i++) { result[i] = MAXN * MAXN; } } int main() { read(); backtrack(0); for (int i = 1; i <= n; i++) { printf("%d %lld\n", result[i], ways[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 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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 45; typedef long long i64; int input[MAXN]; int chosen[MAXN]; int c_size = 0; int inversions = 0; int result[MAXN]; i64 ways[MAXN]; int n; void backtrack(int d) { if (d == n) { return; } int new_inversions = 0; for (int i = 0; i < c_size; i++) { if (chosen[i] > input[d]) { new_inversions++; } } inversions += new_inversions; chosen[c_size] = input[d]; c_size++; if (result[c_size] > inversions) { result[c_size] = inversions; ways[c_size] = 0; } if (result[c_size] == inversions) { ways[c_size]++; } backtrack(d + 1); inversions -= new_inversions; c_size--; backtrack(d + 1); } void read() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &input[i]); } for (int i = 1; i <= n; i++) { result[i] = MAXN * MAXN; } } int main() { read(); backtrack(0); for (int i = 1; i <= n; i++) { printf("%d %lld\n", result[i], ways[i]); } return 0; } |