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
/*
Notice:
- This account submits solutions that result from the collaboration of two (non-Polish) individuals.
- We acknowledge that our participation is completely unofficial.
- We just want to be able to submit our solutions until the end of the week. Thank you :)!
*/
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <fstream>

using namespace std;
typedef long long int64;

const int kInf = 1000;

int n;
vector<int> p;

struct DPState {
    int min;
    int64 num_ways;

    DPState(int min, int64 num_ways) : min(min), num_ways(num_ways) {}

    DPState():  min(kInf), num_ways(0) {}

    void UpdateFrom(const DPState& other, int extra=0) {
        if (other.min + extra < min) {
            min = other.min + extra;
            num_ways = other.num_ways;
        } else if (other.min + extra == min) {
            num_ways += other.num_ways;
        }
    }
};

vector<vector<DPState>> dp;
vector<DPState> final_ans;

pair<vector<int>, int> GetWeightsAndSize(int64 conf) {
    int current_weight = 1;
    int new_group_size = 0;
    vector<int> weights(n);
    // Include n to get a guaranteed 0 at the end and close the last interval
    for (int i = 0; i <= n; ++i) {
        if ((conf >> i)&1) {
            new_group_size += 1;
            weights[i] = current_weight;
        } else {
            if (new_group_size > 0) {
                current_weight *= (new_group_size + 1);
                new_group_size = 0;
            }
        }
    }
    return {weights, current_weight};
}

vector<pair<int, int>> GetSegments(int64 conf) {
    vector<pair<int, int>> segments;
    int last_start = -1;
    // Include n to get a guaranteed 0 at the end and close the last interval
    for (int i = 0; i <= n; ++i) {
        if ((conf >> i)&1) {
            if (last_start == -1) {
                last_start = i;
            }
        } else {
            if (last_start != -1) {
                segments.push_back({last_start, i - 1});
                last_start = -1;
            }
        }
    }
    return segments;
}

// Backtracking data.
vector<int> hereWeights, nextWeights;
vector<pair<int, int>> segments;
int this_element;
int this_dp;
bool last;

void Backtrack(int ind, int state, int inversions, int next, int cnt) {
    if (ind == segments.size()) {
        if (!last) {
            dp[1 - this_dp][next + nextWeights[this_element]].UpdateFrom(dp[this_dp][state], inversions);
            dp[1 - this_dp][next].UpdateFrom(dp[this_dp][state]);
        } else {
            final_ans[cnt + 1].UpdateFrom(dp[this_dp][state], inversions);
            final_ans[cnt].UpdateFrom(dp[this_dp][state]);
        } 
        return;
    }

    Backtrack(ind + 1, state, inversions, next, cnt);

    for (int el = segments[ind].first; el <= segments[ind].second; ++el) {
        state += hereWeights[el];
        next += nextWeights[el];
        if (el > this_element) {
            ++inversions;
        }
        cnt += 1;
        Backtrack(ind + 1, state, inversions, next, cnt);
    }
}

void DP() {
    dp = vector<vector<DPState>>(2);
    dp[0] = vector<DPState>(1);
    this_dp = 0;
    dp[0][0] = DPState(0, 1);
    // cerr << "HERE" << endl;

    int64 conf = 0;
    for (int i = 0; i < n; ++i, this_dp = 1 - this_dp) {
        int size, next_size;
        tie(hereWeights, size) = GetWeightsAndSize(conf);
        tie(nextWeights, next_size) = GetWeightsAndSize(conf | (1LL << p[i]));
        dp[1 - this_dp] = vector<DPState>(next_size);
        segments = GetSegments(conf);
        this_element = p[i];
        if (i == n - 1) {
            last = true;
            final_ans = vector<DPState>(n + 1);
        } else {
            last = false;
        }

        Backtrack(0, 0, 0, 0, 0);
        conf |= (1LL << p[i]);
    }
}


void Solve(istream& cin) {
    cin >> n;
    p = vector<int>(n);

    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        p[i]--;
    }

    DP();

    for (int i = 1; i <= n; ++i) {
        cout << final_ans[i].min << " " << final_ans[i].num_ways << "\n";
    }
}

int main() {
    Solve(cin);
}