#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin >> n;
vector < int > a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
pair < int, long long > ans[n + 1];
for(int i = 1; i <= n; i++){
ans[i].first = 1e9;
}
for(int i = 1; i < (1 << n); i++){
int cur = 0;
for(int j = 0; j < n; j++){
if((i >> j) & 1){
for(int k = j + 1; k < n; k++){
if(((i >> k) & 1) && a[j] > a[k]){
cur += 1;
}
}
}
}
int bits = __builtin_popcount(i);
if(ans[bits].first > cur){
ans[bits] = make_pair(cur, 1);
}
else if(ans[bits].first == cur){
ans[bits].second += 1;
}
}
for(int i = 1; i <= n; i++){
cout << ans[i].first << " " << ans[i].second << "\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 | #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n; cin >> n; vector < int > a(n); for(int i = 0; i < n; i++){ cin >> a[i]; } pair < int, long long > ans[n + 1]; for(int i = 1; i <= n; i++){ ans[i].first = 1e9; } for(int i = 1; i < (1 << n); i++){ int cur = 0; for(int j = 0; j < n; j++){ if((i >> j) & 1){ for(int k = j + 1; k < n; k++){ if(((i >> k) & 1) && a[j] > a[k]){ cur += 1; } } } } int bits = __builtin_popcount(i); if(ans[bits].first > cur){ ans[bits] = make_pair(cur, 1); } else if(ans[bits].first == cur){ ans[bits].second += 1; } } for(int i = 1; i <= n; i++){ cout << ans[i].first << " " << ans[i].second << "\n"; } } |
English