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
#include<bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) (int)(x).size()
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define REP(x, n) for(int x = 0; x < (n); x++)
#define FOR(x, b, e) for(int x = (b); x <= (e); x++)
#define FORD(x, b, e) for(int x = (b); x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)

typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int MXN = 42;
const int C = 262144;
const int INF = 1000000001;

int a[MXN];
int K, L;
int n;
int res[MXN];
LL cnt[MXN];
int mask[MXN];
int mask2[MXN];
VI A, B;

VI gen(int pocz, int kon, VI &res) {
	int len = kon - pocz + 1;
	res.resize(1 << len, 0);
	
	FOR(i, pocz, kon) {
		int m = 0;
		FOR(j, pocz, i)
			if(a[j] > a[i])
				m |= (1 << (len - (j - pocz + 1) ) );
		mask[i - pocz + 1] = m;
	}

	FOR(i, 1, (1 << len) - 1) {
		int k = i & (i - 1);
		int p = i ^ k;
		int count = len - __builtin_ctz(p);
		res[i] = res[k] + __builtin_popcount(k & mask[count]);
	}
	
	return res;
}

//map< pair<int, pair<LL, LL> >, PII> M;
const int ILE = 10;
vector< pair< pair<int, pair<LL, LL> >, PII> > R2;
vector< pair< pair<int, pair<LL, LL> >, int> > R;

// ((krotka, ile elementow w zbiorze), (ile inwersji, ile takich zbiorow) )
void licz(VI &vec) {	
	FOR(i, 1, L) {
		int m = 0;
		FOR(j, 1, K)
			if(a[j] > a[i + K])
				m |= (1 << (K - j) );
		mask2[i] = m;
	}

	R.resize(SIZE(vec), MP( MP(0, MP(0, 0)), 0) );

	FOR(i, 0, SIZE(vec) - 1) {
		int w = vec[i];
		pair<LL, LL> para = MP(0, 0);
		FOR(j, 1, L) {
			LL q = __builtin_popcount(mask2[j] & i);
			if(j <= ILE)
				para.F += q << (5 * (j - 1) );
			else
				para.S += q << (5 * (j - 1 - ILE) );
		}
		pair<int, pair<LL, LL> > p1 = MP(__builtin_popcount(i), para);
		R[i] = (MP(p1, w) );
/*
		if(M.find(p1) == M.end() )
			M[p1] = MP(w, 0);
		PII p2 = M[p1];
	
		if(p2.F == w) {
			p2.S++;
			M[p1] = p2;
		}
		else if(p2.F > w)
			M[p1] = MP(w, 1);*/
	}

	sort(ALL(R) );

	FOR(i, 0, SIZE(R) - 1) {
		int iter = i;
		PII p1 = MP(n * n, 0);

		while(iter < SIZE(R) && R[i].F == R[iter].F) {
			if(R[iter].S < p1.F)
				p1 = MP(R[iter].S, 1);
			else if(R[iter].S == p1.F)
				p1.S++;
			iter++;
		}

		R2.PB(MP(R[i].F, p1) );
		i = iter - 1;
	}
}

const int W = 20;
int cool[(1 << W) + 10];

void solve(VI &vec) {
	FOR(i, 0, (1 << W) - 1) {
		int q = i;
		while(q) {
			cool[i] += q & 31;
			q >>= 5;
		}
	}

	FOR(i, 0, SIZE(vec) - 1) {
		LL kappa1 = 0;
		LL kappa2 = 0;

		FOR(j, 1, ILE)
			if(i & (1 << (L - j) ) )
				kappa1 += 31LL << (5 * (j - 1) );
		FOR(j, ILE + 1, L)
			if(i & (1 << (L - j) ) )
				kappa2 += 31LL << (5 * (j - ILE - 1) );

		FOREACH(it, R2) {
			pair<int, pair<LL, LL> > p1 = it -> F;
			PII p2 = it -> S;
			pair<LL, LL> maska = p1.S;
			int elem = p1.F + __builtin_popcount(i);
			int inv = p2.F + vec[i];
			int count = p2.S;

			maska.F &= kappa1;
			maska.S &= kappa2;
			while(maska.F) {
				inv += cool[maska.F & ( (1 << W) - 1) ];
				maska.F >>= W;
			}
			while(maska.S) {
				inv += cool[maska.S & ( (1 << W) - 1) ];
				maska.S >>= W;
			}


		//	inv += cool[maska.F] + cool[maska.S];

	/*		FOR(o, 1, L) {
				if( (1 << (L - o) ) & i) {
					if(o <= ILE) {
				//		inv += maska.F & 31;
						inv += (maska.F >> (5 * (o - 1) ) ) & 31;
					}
					else {
						//inv += maska.F & 31;
						inv += (maska.S >> (5 * (o - 1 - ILE) ) ) & 31;	
					}
				}
			}*/

			if(res[elem] > inv) {
				res[elem] = inv;
				cnt[elem] = count;
			}
			else if(res[elem] == inv)
				cnt[elem] += count;
		}
	}
}

void test() {
	scanf("%d", &n);
	FOR(i, 1, n)
		scanf("%d", &a[i]);
	FOR(i, 1, n)
		res[i] = n * n;

	K = max(1, (int)(n * 0.56) );
	L = n - K;

	gen(1, K, A);
	gen(K + 1, n, B);
	licz(A);
	solve(B);

	FOR(i, 1, n)
		printf("%d %lld\n", res[i], cnt[i]);
}

int main() {
	int te = 1;
//	scanf("%d", &te);
	while(te--)
		test();
	return 0;
}