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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,x,y) for(int i=(int)(x);i<(int)(y);++i)
#define FORE(i,x,y) for(int i=(int)(x);i<=(int)(y);++i)
#define FORD(i,x,y) for(int i=(int)(x);i>=(int)(y);--i)
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,ll> pill;
typedef pair<ll,ll> p2ll;
const int MAXN=46;
const int INF=2e9;
const int MAXX=(1<<20)+6;

int n,m,s,z=0;
int a[MAXN];
int c[MAXN];
bool e[MAXN][MAXN];
pii parts[MAXN];
int ans[MAXN];
ll ansl[MAXN];
bool vis[MAXN];
vector<p2ll> b[MAXN][2];
ll mask=0;
int inv=0;
int on=0;
bool zap=false;
int pol;
void Solve(int x){
    //cerr<<on<<" "<<inv<<"\n";
    if(zap){
        b[on][pol].PB(MP(inv,mask));
    }else{
        if(ans[on]>inv){
            ans[on]=inv;
            ansl[on]=1;
        }else if(ans[on]==inv){
            ++ansl[on];
        }
    }
    if(x>m){
        return;
    }

    else{
        FORE(i,x,m){
            int y=0;
            FOR(j,s,m){
                if(mask&(1LL<<j)&&e[i][j]){
                    ++y;
                }
            }
            mask|=(1LL<<i);
            inv+=y;
            ++on;
            Solve(i+1);
            --on;
            inv-=y;
            mask&=(~(1LL<<i));
        }
    }
}
int Invs(ll mask2,ll mask1){
    int ciag1[MAXN],c1=0;
    int ciag2[MAXN],c2=0;
    for(int i=0;mask1>0||mask2>0;++i){
        if(mask1&1)ciag1[c1++]=a[i];
        if(mask2&1)ciag2[c2++]=a[i];
        mask1/=2;
        mask2/=2;
    }
    sort(ciag1,ciag1+c1);
    sort(ciag2,ciag2+c2);
    int j=0;
    int res=0;
    FOR(i,0,c1){
        while(j<c2&&ciag2[j]<ciag1[i]){
            ++j;
        }
        res+=j;
    }
    return res;
}
//ll rek=0;
void Meet(){
    zap=true;
    pol=0;
    int p=(s+m)/2;
    int siz0=m-p,siz1=p-s+1;
    Solve(p+1);
    pol=1;
    m=p;
    Solve(s);
    FORE(i,1,siz0+siz1)ans[i]=INF,ansl[i]=0;
    ans[0]=INF;ansl[0]=0;
    FORE(i,0,siz0)sort(b[i][0].begin(),b[i][0].end());
    FORE(j,0,siz1)sort(b[j][1].begin(),b[j][1].end());
    FORE(i,0,siz0){
        int mxid0=b[i][0].size();
        FORE(j,0,siz1){
            int mxid1=b[j][1].size();
            bool flag=false;
            FOR(id0,0,mxid0){
                int inv0=b[i][0][id0].ST;
                if(ans[i+j]<inv0||flag)break;
                flag=true;
                FOR(id1,0,mxid1){
                    //++rek;
                    int inv1=b[j][1][id1].ST;
                    if(ans[i+j]<inv0+inv1){
                        break;
                    }else{
                        flag=false;
                    }
                    inv1+=Invs(b[i][0][id0].ND,b[j][1][id1].ND);
                    if(ans[i+j]==inv0+inv1){
                        ++ansl[i+j];
                    }else if(ans[i+j]>inv0+inv1){
                        ans[i+j]=inv0+inv1;
                        ansl[i+j]=1;
                    }
                }
            }
        }
    }
    zap=false;
}

void BigSolve(int x,int(&res)[MAXN],ll (&resl)[MAXN],int (&rsiz)){
    res[0]=0;
    resl[0]=1;
    rsiz=0;
    if(x>=z)return;


    s=parts[x].ST;
    m=parts[x].ND;
    int asiz=m-s+1,tsiz;
    int tres[MAXN];
    ll tresl[MAXN];

    BigSolve(x+1,tres,tresl,tsiz);
    FORE(i,1,asiz)ans[i]=INF,ansl[i]=0;
    ans[0]=INF;ansl[0]=0;
    s=parts[x].ST;
    m=parts[x].ND;
    if(asiz>200)Meet();//cos co sie nigdy nie wlaczy
    //#warning meet liczba
    else Solve(s);
    rsiz=tsiz+asiz;
    FORE(i,1,rsiz){
        res[i]=INF;
        resl[i]=0;
    }
    resl[0]=0;
    FORE(i,0,asiz){
        FORE(j,0,tsiz){
            if(res[i+j]>ans[i]+tres[j]){
                res[i+j]=ans[i]+tres[j];
                resl[i+j]=ansl[i]*tresl[j];
            }else if(res[i+j]==ans[i]+tres[j]){
                resl[i+j]+=ansl[i]*tresl[j];
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin>>n;
    FORE(i,1,n){
        cin>>a[i];
        FORD(j,i-1,0){
            if(a[j]>a[i]){
                ++c[j];
                ++c[i];
                e[i][j]=true;
                e[j][i]=true;
            }
        }
        ans[i]=0;
        ansl[i]=0;
    }
    queue<int> Q;
    FORE(i,1,n){
        if(!vis[i]){
            vis[i]=true;
            parts[z].ST=i;
            parts[z].ND=i;
            Q.push(i);
            while(!Q.empty()){
                int v=Q.front();Q.pop();
                FORE(w,1,n){
                    if(!vis[w]&&e[v][w]){
                        parts[z].ND=max(parts[z].ND,w);
                        Q.push(w);
                        vis[w]=true;
                    }
                }
            }
            z++;
        }
    }
    /*z=1;
    parts[0].ST=1;
    parts[0].ND=n;*/
    int res[MAXN];
    ll resl[MAXN];
    int rsiz;
    BigSolve(0,res,resl,rsiz);
    FORE(i,1,n){
        cout<<res[i]<<" "<<resl[i]<<"\n";
    }
   // cout<<rek;
    return 0;
}
/*
6
1 6 4 2 5 3
*/