#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int p = 64;
int d[2*p];
void wstaw(int pos){
pos+=p;
d[pos]=1;
pos>>=1;
while(pos>0){
d[pos]=d[pos*2]+d[2*pos+1];
pos>>=1;
}
}
int query(int pos){
int l = pos+p;
int r = 2*p-1;
int sum = 0;
while(l!=r){
if(l&1)
sum+=d[l++];
if(!(r&1))
sum+=d[r--];
l>>=1;
r>>=1;
}
if(l==r)
sum+=d[l];
return sum;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n;
vector<int> t;
cin >> n;
for(int i = 0; i < n; i++){
int x;
cin >> x;
t.push_back(x);
}
vector<pair<int, ll> > odp;
odp.resize(n+1);
for(int i = 1; i <=n;i++)
odp[i]={1e9,1};
int j;
for(int mask = 1; mask < (1<<n); mask++){
memset(d, 0, 2*p*sizeof(int));
j = mask;
int lp = 0;
set<int> v;
int pos = 0;
int inw = 0;
while(j>0){
if(j&1){
lp++;
inw+=query(t[pos]);
wstaw(t[pos]);
}
pos++;
j>>=1;
}
if(odp[lp].first>inw)
odp[lp]={inw,0};
if(odp[lp].first==inw)
odp[lp].second++;
}
for(int i = 1; i<=n; i++)
cout << odp[i].first << " " << odp[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 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int p = 64; int d[2*p]; void wstaw(int pos){ pos+=p; d[pos]=1; pos>>=1; while(pos>0){ d[pos]=d[pos*2]+d[2*pos+1]; pos>>=1; } } int query(int pos){ int l = pos+p; int r = 2*p-1; int sum = 0; while(l!=r){ if(l&1) sum+=d[l++]; if(!(r&1)) sum+=d[r--]; l>>=1; r>>=1; } if(l==r) sum+=d[l]; return sum; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; vector<int> t; cin >> n; for(int i = 0; i < n; i++){ int x; cin >> x; t.push_back(x); } vector<pair<int, ll> > odp; odp.resize(n+1); for(int i = 1; i <=n;i++) odp[i]={1e9,1}; int j; for(int mask = 1; mask < (1<<n); mask++){ memset(d, 0, 2*p*sizeof(int)); j = mask; int lp = 0; set<int> v; int pos = 0; int inw = 0; while(j>0){ if(j&1){ lp++; inw+=query(t[pos]); wstaw(t[pos]); } pos++; j>>=1; } if(odp[lp].first>inw) odp[lp]={inw,0}; if(odp[lp].first==inw) odp[lp].second++; } for(int i = 1; i<=n; i++) cout << odp[i].first << " " << odp[i].second << "\n"; } |
English