//#pragma GCC optimize ("Ofast") #pragma GCC target ("avx")// #include <bits/stdc++.h> using namespace std; typedef long long int LL; typedef unsigned long long int uLL; typedef unsigned int uint; typedef long double ld; const int N = 45; bitset <N> tab, vis; int minim, n, wyn; LL ways; vector <int> V[N]; int P[N], wal[N]; pair<int,int> opt[N]; void back_tracking(int pos, int k){ if(wyn > minim){ return; } if(k == 0){ ways = (ways+1)*(wyn == minim)+(wyn < minim); minim = min(wyn, minim); return; } if(k > 0){ tab[pos] = true; int v = P[pos]; vis[v] = true; int zz = 0; for(auto u: V[v]){ if(vis[u] == true){ ++zz; } } wyn += zz; back_tracking(pos+1, k-1); wyn -= zz; vis[v] = false; tab[pos] = false; } if(n-pos >= k){ back_tracking(pos+1, k); } return; } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> n; for(int i = 1; i <= n; i++){ int a; cin >> a; P[i] = i; wal[i] = a; for(int j = 1; j < i; j++){ if(wal[j] > a){ V[i].push_back(j); V[j].push_back(i); } } } for(int i = 1; i <= n; i++){ opt[i] = {V[i].size(), i}; } sort(opt+1, opt+n+1); for(int i = 1; i <= n; i++){ P[i] = opt[n-i+1].second; } //srand(P[n]); //random_shuffle(P+1, P+n+1); for(int i = 1; i <= n; i++){ minim = 100000; ways = 0; back_tracking(1, i); cout << minim << " " << ways << '\n'; } }
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | //#pragma GCC optimize ("Ofast") #pragma GCC target ("avx")// #include <bits/stdc++.h> using namespace std; typedef long long int LL; typedef unsigned long long int uLL; typedef unsigned int uint; typedef long double ld; const int N = 45; bitset <N> tab, vis; int minim, n, wyn; LL ways; vector <int> V[N]; int P[N], wal[N]; pair<int,int> opt[N]; void back_tracking(int pos, int k){ if(wyn > minim){ return; } if(k == 0){ ways = (ways+1)*(wyn == minim)+(wyn < minim); minim = min(wyn, minim); return; } if(k > 0){ tab[pos] = true; int v = P[pos]; vis[v] = true; int zz = 0; for(auto u: V[v]){ if(vis[u] == true){ ++zz; } } wyn += zz; back_tracking(pos+1, k-1); wyn -= zz; vis[v] = false; tab[pos] = false; } if(n-pos >= k){ back_tracking(pos+1, k); } return; } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> n; for(int i = 1; i <= n; i++){ int a; cin >> a; P[i] = i; wal[i] = a; for(int j = 1; j < i; j++){ if(wal[j] > a){ V[i].push_back(j); V[j].push_back(i); } } } for(int i = 1; i <= n; i++){ opt[i] = {V[i].size(), i}; } sort(opt+1, opt+n+1); for(int i = 1; i <= n; i++){ P[i] = opt[n-i+1].second; } //srand(P[n]); //random_shuffle(P+1, P+n+1); for(int i = 1; i <= n; i++){ minim = 100000; ways = 0; back_tracking(1, i); cout << minim << " " << ways << '\n'; } } |