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
//O(2^(3n/4) * n)

#include <bits/stdc++.h>

using namespace std;

using ll = long long int;

const int MAXN = 45;

int perm[MAXN];

int group1[MAXN];
int group2[MAXN];
int group3[MAXN];

ll neigh[MAXN];

int outmin[MAXN];
ll outcnt[MAXN];

int optedges[MAXN];
ll optcount[MAXN];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &perm[i]);
        perm[i]--;
    }

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (perm[i] > perm[j]) {
                neigh[i] ^= (1ll << j);
                neigh[j] ^= (1ll << i);
            }
        }
    }

    for (int i = 1; i <= n; i++) outmin[i] = n*n*n;

    int bestmin = n;
    int bestt = 0;
    int bestp = 0;
    int besth = 0;
    //find a partition
    for (int p = 0; p <= n; p++) {
        for (int t = 0; t <= n; t++) {
            int cntl = 0;
            int cntr = 0;
            for (int i = 0; i < p; i++)
                if (perm[i] <= t) cntl++;
            for (int i = p + 1; i < n; i++)
                if (perm[i] > t) cntr++;
            int option1 = max(n - cntl, n - cntr);
            int option2 = max(n - (p - cntl), n - (n - p - cntr));
            if (bestmin > option1) {
                bestmin = option1;
                bestt = t;
                bestp = p;
                besth = 0;
            }
            if (bestmin > option2) {
                bestmin = option2;
                bestt = t;
                bestp = p;
                besth = 1;
            }
        }
    }


    //groups construction
    int g1size = 0;
    int g2size = 0;
    int g3size = 0;
    ll g1mask = 0;
    ll g2mask = 0;
    ll g3mask = 0;
    for (int i = 0; i < n; i++) {
        if (i < bestp) {
            if ((perm[i] <= bestt) ^ besth) {
                group2[g2size] = i;
                g2size++;
                g2mask ^= (1LL << i);
            }
            else {
                group1[g1size] = i;
                g1size++;
                g1mask ^= (1LL << i);
            }
        }
        else {
            if ((perm[i] > bestt) ^ besth) {
                group3[g3size] = i;
                g3size++;
                g3mask ^= (1LL << i);
            }
            else {
                group1[g1size] = i;
                g1size++;
                g1mask ^= (1LL << i);
            }
        }
    }

    //meet in the middle
    for (ll mask1 = g1mask; ; mask1 = (mask1 - 1) & g1mask) {
        for (int i = 0; i <= n; i++)
            optedges[i] = n * n * n;
        int mask1edges = 0;
        int mask1verts = __builtin_popcountll(mask1);
        for (int i = 0; i < g1size; i++)
            if ((1LL << group1[i]) & mask1)
                mask1edges += __builtin_popcountll(mask1 & neigh[group1[i]]);
        mask1edges >>= 1;

        for (ll mask2 = g2mask; ; mask2 = (mask2 - 1) & g2mask) {
            int mask2edges = 0;
            int mask2verts = __builtin_popcountll(mask2);
            for (int i = 0; i < g2size; i++)
                if ((1LL << group2[i]) & mask2) {
                    mask2edges += __builtin_popcountll(mask2 & neigh[group2[i]]);
                    mask2edges += (__builtin_popcountll(mask1 & neigh[group2[i]]) << 1);
                }
            mask2edges >>= 1;

            if (optedges[mask2verts] > mask2edges) {
                optedges[mask2verts] = mask2edges;
                optcount[mask2verts] = 1;
            }
            else if (optedges[mask2verts] == mask2edges) {
                optcount[mask2verts]++;
            }

            if (mask2 == 0) break;
        }

        for (ll mask3 = g3mask; ; mask3 = (mask3 - 1) & g3mask) {
            int mask3edges = 0;
            int mask3verts = __builtin_popcountll(mask3);
            for (int i = 0; i < g3size; i++)
                if ((1LL << group3[i]) & mask3) {
                    mask3edges += __builtin_popcountll(mask3 & neigh[group3[i]]);
                    mask3edges += (__builtin_popcountll(mask1 & neigh[group3[i]]) << 1);
                }
            mask3edges >>= 1;

            for (int i = 0; i <= g2size; i++) {
                int totaledges = mask1edges + optedges[i] + mask3edges;
                if (besth)
                    totaledges += mask3verts * i;
                int totalverts = mask1verts + i + mask3verts;

                if (totaledges < outmin[totalverts]) {
                    outmin[totalverts] = totaledges;
                    outcnt[totalverts] = optcount[i];
                }
                else if (totaledges == outmin[totalverts]) {
                    outcnt[totalverts] += optcount[i];
                }
            }

            if (mask3 == 0) break;
        }

        if (mask1 == 0) break;
    }

    for (int i = 1; i <= n; i++) {
        printf("%d %lld\n", outmin[i], outcnt[i]);
    }
}