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


using namespace std;


const int INF = 1 << 30;

vector <pair<int,int>> addAndMerge(vector <pair<int,int>> parts, int x) {
    parts.push_back({x, x});
    sort(parts.begin(), parts.end());

    vector <pair<int,int>> partsNew;
    for (pair <int,int> part : parts) {
        while (!partsNew.empty() && partsNew.back().second + 1 == part.first) {
            part.first = partsNew.back().first;
            partsNew.pop_back();
        }

        partsNew.push_back(part);
    }

    return partsNew;
}

vector <int> computePartSizes(const vector <pair<int,int>> &parts) {
    vector <int> partSize(parts.size());

    for (int i = 0; i < parts.size(); i++) {
        partSize[i] = parts[i].second - parts[i].first + 2;
    }

    return partSize;
}

vector <int> computePrefixProducts(const vector <int> &vec) {
    vector <int> prods(vec.size() + 1);
    prods[0] = 1;

    for (int i = 0; i < vec.size(); i++) {
        prods[i + 1] = prods[i] * vec[i];
    }

    return prods;
}

vector <pair<int,long long>> solve(int n, vector <int> &perm) {
    vector <int> inPerm(n);
    for (int i = 0; i < n; i++) {
        inPerm[perm[i]] = i;
    }

    vector <pair<int,long long>> dp = {{0, 1}, {0, 1}};
    vector <pair<int,int>> parts = {{inPerm[0], inPerm[0]}};

    vector <int> partSize = computePartSizes(parts);
    vector <int> muls = computePrefixProducts(partSize);

    for (int x = 1; x < n; x++) {
        int xPos = inPerm[x];

        int suff = parts.size();
        while (suff > 0 && parts[suff - 1].first > xPos) suff--;

        auto partsNew = addAndMerge(parts, xPos);

        auto findInNewParts = [&](int p) {
            for (int i = 0; i < partsNew.size(); i++) {
                if (partsNew[i].first <= p && p <= partsNew[i].second) {
                    return i;
                }
            }

            assert(false);
        };

        int xToNew = findInNewParts(xPos);

        vector <int> oldToNew(parts.size());
        for (int i = 0; i < parts.size(); i++) {
            oldToNew[i] = findInNewParts(parts[i].first);
        }

        vector <int> partSizeNew = computePartSizes(partsNew);
        vector <int> mulsNew = computePrefixProducts(partSizeNew);

        vector <pair<int,long long>> dpNew(mulsNew.back(), {INF, 0});

        auto addSolution = [&](int sign, int inv, long long ways) {
            if (dpNew[sign].first > inv) {
                dpNew[sign] = {inv, ways};
            } else if (dpNew[sign].first == inv) {
                dpNew[sign].second += ways;
            }
        };

        for (int base = 0; base < dp.size(); base += muls[suff]) {
            int signAdd = 0;
            int invAdd = 0;

            for (int i = suff; i < parts.size(); i++) {
                int signValue = (base / muls[i]) % partSize[i];

                signAdd += signValue * mulsNew[oldToNew[i]];
                invAdd += signValue;
            }

            for (int rem = 0; rem < muls[suff]; rem++) {
                int sign = base + rem;

                int inv; long long ways;
                tie(inv, ways) = dp[sign];

                int signNew = signAdd + rem;

                addSolution(signNew, inv, ways);
                addSolution(signNew + mulsNew[xToNew], inv + invAdd, ways);
            }
        }

        dp = dpNew;
        parts = partsNew;

        muls = mulsNew;
        partSize = partSizeNew;
    }

    dp.erase(dp.begin());
    return dp;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

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

    auto ans = solve(n, perm);

    for (int i = 0; i < n; i++) {
        cout << ans[i].first << ' ' << ans[i].second << '\n';
    }

    return 0;
}