結果

問題 No.1087 転倒数の転倒数
ユーザー risujirohrisujiroh
提出日時 2020-06-19 23:22:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,743 bytes
コンパイル時間 2,755 ms
コンパイル使用メモリ 208,384 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-16 15:40:15
合計ジャッジ時間 21,154 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 126 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 125 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 103 ms
4,376 KB
testcase_22 WA -
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 7 ms
4,376 KB
testcase_25 AC 94 ms
4,376 KB
testcase_26 AC 84 ms
4,380 KB
testcase_27 AC 53 ms
4,376 KB
testcase_28 AC 48 ms
4,376 KB
testcase_29 AC 66 ms
4,380 KB
testcase_30 AC 31 ms
4,380 KB
testcase_31 AC 92 ms
4,380 KB
testcase_32 AC 91 ms
4,380 KB
testcase_33 AC 56 ms
4,376 KB
testcase_34 AC 126 ms
4,380 KB
testcase_35 AC 124 ms
4,376 KB
testcase_36 AC 127 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct fenwick {
  int n;
  vector<T> t;
  fenwick(int _n = -1) : n(_n), t(n + 1) {}
  void add(int i, T a) { for (++i; i <= n; i += i & -i) t[i] += a; }
  T sum(int i) const {
    T s = 0;
    for (; i; i -= i & -i) s += t[i];
    return s;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  int kth(T k) const {
    int i = 0;
    for (int w = 1 << __lg(n); w; w >>= 1)
      if (i + w <= n and t[i + w] <= k) k -= t[i += w];
    return i;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  auto f = [](int n, int x) {
    vector<int> ord(n), a(n);
    for (int i = 0; i < n; ++i) {
      int t = min(x, i);
      ord[i] = i - t;
      x -= t;
    }
    fenwick<int> ft(n);
    for (int i = 0; i < n; ++i) {
      ft.add(i, 1);
    }
    for (int i = n; i--; ) {
      a[i] = ft.kth(ord[i]);
      ft.add(a[i], -1);
    }
    return a;
  };
  int n, k;
  cin >> n >> k;
  if (k > n * (n - 1)) {
    cout << "No\n";
    exit(0);
  }
  cout << "Yes\n";
  if (k <= n * (n - 1) / 2) {
    auto row = f(n, k);
    for (int i = 0; i < n; ++i) {
      auto a = f(n, row[i]);
      for (int j = 0; j < n; ++j) {
        cout << i * n + a[j] << " \n"[j == n - 1];
      }
    }
    exit(0);
  }
  if (n < 4) {
    vector<int> p(n * n);
    iota(begin(p), end(p), 0);
    do {
      vector a(n, vector<int>(n));
      vector<int> row(n), col(n);
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          a[i][j] = p[i * n + j];
        }
        for (int y = 0; y < n; ++y) {
          for (int x = 0; x < y; ++x) {
            row[i] += a[i][x] > a[i][y];
          }
        }
      }
      for (int j = 0; j < n; ++j) {
        for (int y = 0; y < n; ++y) {
          for (int x = 0; x < y; ++x) {
            col[j] += a[x][j] > a[y][j];
          }
        }
      }
      int t = 0;
      for (int y = 0; y < n; ++y) {
        for (int x = 0; x < y; ++x) {
          t += row[x] > row[y];
          t += col[x] > col[y];
        }
      }
      if (t == k) {
        for (int i = 0; i < n; ++i) {
          for (int j = 0; j < n; ++j) {
            cout << a[i][j] << " \n"[j == n - 1];
          }
        }
        break;
      }
    } while (next_permutation(begin(p), end(p)));
    exit(0);
  }
  k -= n * (n - 1) / 2;
  auto row = f(n, k);
  for (int i = 0; i < n; ++i) {
    auto a = f(n - 1, i + row[i]);
    for (int j = 0; j < n; ++j) {
      int cur = 0;
      if (j < n - i - 1) {
        cur = i * n + a[j] + 1;
      } else if (j > n - i - 1) {
        cur = i * n + a[j - 1] + 1;
      }
      cout << cur << " \n"[j == n - 1];
    }
  }
}
0