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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

struct Point {
  int x,y;
};

inline bool inverted(const Point &a, const Point &b) {
  return (a.x < b.x) != (a.y < b.y);
}

vector<Point> quadrants[2][2];
bool flipped;

void read_points() {
  int n; cin >> n;
  REP(i,2) REP(j,2) quadrants[i][j].clear();
  int threshold = n/2;
  REP(x,n) {
    int y; cin >> y; --y;
    quadrants[x>=threshold][y>=threshold].push_back(Point{x,y});
  }
}

void main_diagonal_light() {
  // move smallest quadrant to main diagonal
  flipped =
    min(quadrants[0][0].size(), quadrants[1][1].size()) >
    min(quadrants[0][1].size(), quadrants[1][0].size());

  if (flipped) {
    swap(quadrants[0][0], quadrants[0][1]);
    swap(quadrants[1][0], quadrants[1][1]);
  }
}

// For at most 20. 20*20/2 = 200 inversions.
struct SmallCountAndInversions {
  uint8_t count;
  uint8_t inversions;
};

inline SmallCountAndInversions operator-(const SmallCountAndInversions &a,
                                         const SmallCountAndInversions &b) {
  return SmallCountAndInversions{uint8_t(a.count-b.count), uint8_t(a.inversions-b.inversions)};
}

inline void operator-=(SmallCountAndInversions &a, const SmallCountAndInversions &b) {
  a = a-b;
}

inline SmallCountAndInversions operator+(const SmallCountAndInversions &a,
                                         const SmallCountAndInversions &b) {
  return SmallCountAndInversions{uint8_t(a.count+b.count), uint8_t(a.inversions+b.inversions)};
}

inline void operator+=(SmallCountAndInversions &a, const SmallCountAndInversions &b) {
  a = a+b;
}

inline bool operator==(const SmallCountAndInversions &a, const SmallCountAndInversions &b) {
  return a.count == b.count &&
    a.inversions == b.inversions;
}

SmallCountAndInversions brute_force_count_and_inversions(const vector<Point> &points, unsigned subset) {
  SmallCountAndInversions res{0,0};
  int n = points.size();
  REP(i,n) if(subset&(1u<<i)) {
    ++res.count;
    REP(j,i) if(subset&(1u<<j)) {
      res.inversions += inverted(points[i], points[j]);
    }
  }
  return res;
}

vector<SmallCountAndInversions> brute_force_tabulate_count_and_inversions(const vector<Point> &points) {
  int n = points.size();
  vector<SmallCountAndInversions> res(1u<<n);
  for(unsigned subset=0; subset < (1u<<n); ++subset) {
    res[subset] = brute_force_count_and_inversions(points, subset);
  }
  return res;
}

vector<SmallCountAndInversions> tabulate_count_and_inversions(const vector<Point> &points) {
  int n = points.size();
  int splits[4];
  REP(i,4) splits[i] = n*i/3;

  vector<Point> subpoints[3];
  REP(i,3) subpoints[i].assign(points.begin() + splits[i], points.begin() + splits[i+1]);

  int ss[3];
  REP(i,3) ss[i] = subpoints[i].size();

  vector<SmallCountAndInversions> subtables[3];
  REP(i,3) subtables[i] = brute_force_tabulate_count_and_inversions(subpoints[i]);

  vector<SmallCountAndInversions> subtables2[3];
  REP(i,3) {
    vector<Point> v = subpoints[i];
    int i2 = (i+1)%3;
    v.insert(v.end(), subpoints[i2].begin(), subpoints[i2].end());
    subtables2[i] = brute_force_tabulate_count_and_inversions(v);
    for(unsigned sub1 = 0; sub1 < (1u<<ss[i2]); ++sub1) {
      for(unsigned sub0 = 0; sub0 < (1u<<ss[i]); ++sub0) {
        subtables2[i][sub0 | (sub1 << ss[i])] -= subtables[i][sub0];
      }
    }
  }

  vector<SmallCountAndInversions> res(1u<<n);
  for (unsigned sub2=0; sub2 < (1u<<ss[2]); ++sub2) {
    for (unsigned sub1=0; sub1 < (1u<<ss[1]); ++sub1) {
      for (unsigned sub0=0; sub0 < (1u<<ss[0]); ++sub0) {
        res[sub0 | (sub1 << splits[1]) | (sub2 << splits[2])] =
          subtables2[0][sub0 | (sub1 << ss[0])] +
          subtables2[1][sub1 | (sub2 << ss[1])] +
          subtables2[2][sub2 | (sub0 << ss[2])];
      }
    }
  }
  return res;
}

vector<SmallCountAndInversions> quadrant_count_and_inversions[2][2];
// Does not include main diagonal counts and inversions.
vector<SmallCountAndInversions> horizontal_count_and_inversions[2];
vector<uint8_t> vertical_cross_inversions[2];

void calculate_tables() {
  REP(i,2) REP(j,2) {
    quadrant_count_and_inversions[i][j] = tabulate_count_and_inversions(quadrants[i][j]);
  }
  REP(i,2) {
    const vector<Point> &offdiag = quadrants[i][i^1];
    const vector<Point> &maindiag = quadrants[i^1][i^1];
    int noff = offdiag.size();
    int nmain = maindiag.size();
    vector<Point> v = offdiag;
    v.insert(v.end(), maindiag.begin(), maindiag.end());
    horizontal_count_and_inversions[i] = tabulate_count_and_inversions(v);
    for (unsigned sub1=0; sub1 < (1u<<nmain); ++sub1) {
      for (unsigned sub0=0; sub0 < (1u<<noff); ++sub0) {
        horizontal_count_and_inversions[i][sub0 | (sub1<<noff)] -=
          quadrant_count_and_inversions[i^1][i^1][sub1];
      }
    }
  }
  REP(i,2) {
    const vector<Point> &offdiag = quadrants[i][i^1];
    const vector<Point> &maindiag = quadrants[i][i];
    int noff = offdiag.size();
    int nmain = maindiag.size();
    vector<Point> v = offdiag;
    v.insert(v.end(), maindiag.begin(), maindiag.end());
    const auto tab = tabulate_count_and_inversions(v);
    vertical_cross_inversions[i].resize(tab.size());
    for (unsigned sub1=0; sub1 < (1u<<nmain); ++sub1) {
      for (unsigned sub0=0; sub0 < (1u<<noff); ++sub0) {
        const unsigned subset = sub0 | (sub1<<noff);
        vertical_cross_inversions[i][subset] =
          tab[subset].inversions -
          quadrant_count_and_inversions[i][i^1][sub0].inversions -
          quadrant_count_and_inversions[i][i][sub1].inversions;
      }
    }
  }
}

struct SmallInversionPossibilities {
  int inversions;
  int possibilities;
};

using SmallInversionPossibilitiesTable = std::array<SmallInversionPossibilities, 21>;

SmallInversionPossibilitiesTable calc_quadrant_possibilities(
    int x,
    unsigned hor_subset,
    unsigned ver_subset) {
  int n = quadrants[x][x^1].size();
  SmallInversionPossibilitiesTable res;
  FOR(i,0,n) res[i] = SmallInversionPossibilities{1000000,0};
  const unsigned subset_limit = 1u << n;
  const SmallCountAndInversions *htable =
    horizontal_count_and_inversions[x].data() + (hor_subset << n);
  const uint8_t *vtable =
    vertical_cross_inversions[x].data() + (ver_subset << n);

  // Inner loop!
#define LOOP_ITER \
      {                                                       \
        const SmallCountAndInversions &he = htable[subset];   \
        const int inv = he.inversions + vtable[subset];       \
        SmallInversionPossibilities &sip = res[he.count];     \
        if (inv <= sip.inversions) {                          \
          if (inv < sip.inversions) sip.possibilities = 0;    \
          sip.inversions = inv;                               \
          ++sip.possibilities;                                \
        }                                                     \
      }                                                       \
      ++subset;
  if (n>=4) {
    for (unsigned subset=0; subset < subset_limit;) {
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
      LOOP_ITER
    }
  } else {
    for (unsigned subset=0; subset < subset_limit;) {
      LOOP_ITER
    }
  }
#undef LOOP_ITER

  return res;
}

struct LargeInversionPossibilities {
  int inversions;
  LL possibilities;
};

using LargeInversionPossibilitiesTable = std::array<LargeInversionPossibilities, 41>;

LargeInversionPossibilitiesTable calc_possibilities() {
  int n00 = quadrants[0][0].size();
  int n01 = quadrants[0][1].size();
  int n10 = quadrants[1][0].size();
  int n11 = quadrants[1][1].size();
  LargeInversionPossibilitiesTable res;
  FOR(i,0,n00+n01+n10+n11) res[i] = LargeInversionPossibilities{1000000, 0};
  for (unsigned sub00 = 0; sub00 < (1u<<n00); ++sub00) {
    const SmallCountAndInversions &sci00 = quadrant_count_and_inversions[0][0][sub00];
    for (unsigned sub11 = 0; sub11 < (1u<<n11); ++sub11) {
      const SmallCountAndInversions &sci11 = quadrant_count_and_inversions[1][1][sub11];
      int inv_main = sci00.inversions + sci11.inversions;
      if (flipped) inv_main += sci00.count * sci11.count;
      int count_main = sci00.count + sci11.count;

      const SmallInversionPossibilitiesTable tab01 =
        calc_quadrant_possibilities(0, sub11, sub00);
      const SmallInversionPossibilitiesTable tab10 =
        calc_quadrant_possibilities(1, sub00, sub11);
      if (flipped) {
        FOR(c01, 0, n01) {
          const auto &tab01entry = tab01[c01];
          FOR(c10, 0, n10) {
            const auto &tab10entry = tab10[c10];
            int inv = inv_main + tab01entry.inversions + tab10entry.inversions;
            //if (!flipped) inv += c01 * c10;
            int cnt = count_main + c01 + c10;
            LargeInversionPossibilities &lip = res[cnt];
            if (inv <= lip.inversions) {
              if (inv < lip.inversions) lip.possibilities = 0;
              lip.inversions = inv;
              lip.possibilities += LL(tab01entry.possibilities) * LL(tab10entry.possibilities);
            }
          }
        }
      } else {
        FOR(c01, 0, n01) {
          const auto &tab01entry = tab01[c01];
          FOR(c10, 0, n10) {
            const auto &tab10entry = tab10[c10];
            int inv = inv_main + tab01entry.inversions + tab10entry.inversions;
            //if (!flipped) inv += c01 * c10;
            inv += c01 * c10;
            int cnt = count_main + c01 + c10;
            LargeInversionPossibilities &lip = res[cnt];
            if (inv <= lip.inversions) {
              if (inv < lip.inversions) lip.possibilities = 0;
              lip.inversions = inv;
              lip.possibilities += LL(tab01entry.possibilities) * LL(tab10entry.possibilities);
            }
          }
        }
      }
    }
  }
  return res;
}

void print_results(const LargeInversionPossibilitiesTable &res) {
  int n = 0;
  REP(i,2) REP(j,2) n += quadrants[i][j].size();
  FOR(cnt,1,n) cout << res[cnt].inversions << " " << res[cnt].possibilities << "\n";
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  read_points();
  main_diagonal_light();
  calculate_tables();
  const auto res = calc_possibilities();
  print_results(res);

  return 0;
}