#include <iostream> #include <vector> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> iset; void R(int k, int inv, int n, iset &S, const vector<int>&Perm, vector<int>&BestInv, vector<long long>&BestCnt){ if(k==n){ int x = (int)S.size(); if(!x) return; if(inv<BestInv[x]){ BestInv[x]=inv; BestCnt[x]=1; } else if(inv==BestInv[x]) BestCnt[x]++; } else if(k<n){ R(k+1, inv, n, S, Perm, BestInv, BestCnt); int new_invs = S.size() - S.order_of_key(Perm[k]); S.insert(Perm[k]); R(k+1, inv+new_invs, n, S, Perm, BestInv, BestCnt); S.erase(Perm[k]); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>A(n); for(int &x : A) cin >> x; vector<int>BestInv(n+1, 1000000000); vector<long long> BestCount(n+1); iset S; R(0, 0, n, S, A, BestInv, BestCount); for(int i=1;i<=n;i++) cout << BestInv[i] << " " << BestCount[i] << "\n"; 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 | #include <iostream> #include <vector> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> iset; void R(int k, int inv, int n, iset &S, const vector<int>&Perm, vector<int>&BestInv, vector<long long>&BestCnt){ if(k==n){ int x = (int)S.size(); if(!x) return; if(inv<BestInv[x]){ BestInv[x]=inv; BestCnt[x]=1; } else if(inv==BestInv[x]) BestCnt[x]++; } else if(k<n){ R(k+1, inv, n, S, Perm, BestInv, BestCnt); int new_invs = S.size() - S.order_of_key(Perm[k]); S.insert(Perm[k]); R(k+1, inv+new_invs, n, S, Perm, BestInv, BestCnt); S.erase(Perm[k]); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int>A(n); for(int &x : A) cin >> x; vector<int>BestInv(n+1, 1000000000); vector<long long> BestCount(n+1); iset S; R(0, 0, n, S, A, BestInv, BestCount); for(int i=1;i<=n;i++) cout << BestInv[i] << " " << BestCount[i] << "\n"; return 0; } |