#include <bits/stdc++.h> using namespace std; int n,ile; vector<int>tmp,mini(41),a,res(41); int getInvCount() { set<int> set1; set1.insert(a[tmp[0]]); int invcount = 0; set<int>::iterator itset1; for (int i=1; i<tmp.size(); i++) { set1.insert(a[tmp[i]]); itset1 = set1.upper_bound(a[tmp[i]]); invcount += distance(itset1, set1.end()); } return invcount; } void solve(){ for(int i=1; i<(1<<n); i++){ tmp.clear(); ile=0; for(int j=0; j<n; j++){ if(i&(1<<j)){ tmp.push_back(j+1); ile++; } } int akt=getInvCount(); if(akt==mini[ile]){ res[ile]++; }else if(akt<mini[ile]){ mini[ile]=akt; res[ile]=1; } } } int main() { cin >> n; a.resize(n+1); for(int i=1; i<=n; i++){ cin >> a[i]; mini[i]=10000000; } solve(); for(int i=1; i<=n; i++) cout << mini[i] << ' ' << res[i]<<endl; 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 | #include <bits/stdc++.h> using namespace std; int n,ile; vector<int>tmp,mini(41),a,res(41); int getInvCount() { set<int> set1; set1.insert(a[tmp[0]]); int invcount = 0; set<int>::iterator itset1; for (int i=1; i<tmp.size(); i++) { set1.insert(a[tmp[i]]); itset1 = set1.upper_bound(a[tmp[i]]); invcount += distance(itset1, set1.end()); } return invcount; } void solve(){ for(int i=1; i<(1<<n); i++){ tmp.clear(); ile=0; for(int j=0; j<n; j++){ if(i&(1<<j)){ tmp.push_back(j+1); ile++; } } int akt=getInvCount(); if(akt==mini[ile]){ res[ile]++; }else if(akt<mini[ile]){ mini[ile]=akt; res[ile]=1; } } } int main() { cin >> n; a.resize(n+1); for(int i=1; i<=n; i++){ cin >> a[i]; mini[i]=10000000; } solve(); for(int i=1; i<=n; i++) cout << mini[i] << ' ' << res[i]<<endl; return 0; } |