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
#define make_pair mp
#define emplace_back pb
#include <bits/stdc++.h>
using namespace std;
const int N = 45, INF = 900, M = 1e6 + 5e5;
const long long S = 1000;
int n, a[N], trie[M][40];
vector<long long> vals[M], EMPTY;

long long P(int mini, long long cnt) {
	return S * cnt + mini;
}

long long cnt(long long &x) {
	return x / S;
}

int mini(long long x) {
	return x % S;
}

int siz = 1;

vector<long long> &value(vector<int> &v) {
	int y = 1;
	for(auto x : v) y = trie[y][x];
	return vals[y];
}

void add(vector<int> &v, vector<long long> &val) {
	int y = 1;
	for(auto x : v) {
		if(trie[y][x] == 0) {
			siz++;
			trie[y][x] = siz;
		}
		y = trie[y][x];
	}
	vals[y] = val;
}

vector<int> weig;

void modify(vector<long long> &ans, int &w) {
	for(int i=n;i>=1;i--) ans[i] = P(w + mini(ans[i-1]), cnt(ans[i-1]));
	ans[0] = P(INF, 0);
}

void add_weig(int &id) {
	for(int i=id+1;i<=n;i++) if(a[id] > a[i]) weig[n-i]++;
}

void remove_weig(int &id) {
	for(int i=id+1;i<=n;i++) if(a[id] > a[i]) weig[n-i]--;
}

void minimize(vector<long long> &ans, vector<long long> &x) {
	for(int i=0;i<=n;i++) if(mini(ans[i]) == mini(x[i])) ans[i] = P(mini(x[i]), cnt(ans[i]) + cnt(x[i]));
	else if(mini(ans[i]) > mini(x[i])) ans[i] = x[i];
}

void clear() {
	for(int i=2;i<=siz;i++) {
		vals[i].clear();
		for(int j=0;j<n;j++) trie[i][j] = 0;
	}
	for(int j=0;j<n;j++) trie[1][j] = 0;
	siz = 2;
}

vector<long long> rec(int id) {
	vector<long long> res = value(weig);
	if(!res.empty()) return res;

	int w = weig.back();
	weig.pop_back();
	vector<long long> ans = rec(id + 1);

	add_weig(id);

	vector<long long> x = rec(id + 1); 
	modify(x, w);

	remove_weig(id);
	weig.pb(w);

	minimize(ans, x);
	add(weig, ans);
	return ans;
}

int Z = 3;

vector<long long> rec_bez(int id) {
	if(id == Z) {
		clear();
		return rec(id);
	}
	int w = weig.back();
	weig.pop_back();
	vector<long long> ans = rec_bez(id + 1);

	add_weig(id);

	vector<long long> x = rec_bez(id + 1);
	modify(x, w);

	remove_weig(id);
	weig.pb(w);

	minimize(ans, x);
	return ans;
}

int main()
{
	scanf("%d", &n);
	for(int i=1;i<=n;i++) scanf("%d", &a[i]);
	if(n == 40) Z = 5;
	Z = min(Z, n);

	vector<long long> z;
	z.pb(P(0, 1));
	for(int i=1;i<=n;i++) z.pb(P(INF, 0));
	add(weig, z);
	for(int j=0;j<n;j++) weig.pb(0);

	vector<long long> ans = rec_bez(1);
	for(int i=1;i<=n;i++) printf("%d %lld\n", mini(ans[i]), cnt(ans[i]));

	return 0;
}