結果

問題 No.3724 Domination
コンテスト
ユーザー Kude
提出日時 2026-09-19 14:02:38
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 56 ms / 2,000 ms
+ 804µs
コード長 2,780 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,783 ms
コンパイル使用メモリ 365,736 KB
実行使用メモリ 10,032 KB
最終ジャッジ日時 2026-09-19 14:02:53
合計ジャッジ時間 14,269 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % AC * 8
満点 80 % AC * 52
合計 2.5 * 100% = 250 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    VI r(n), c(n);
    rep(i, n) cin >> r[i], r[i]--;
    rep(i, n) cin >> c[i], c[i]--;
    vector<P> cj(n);
    rep(j, n) cj[j] = {c[j], j};
    sort(all(cj));
    bool ok = true;
    rep(j, n) ok &= cj[j].first == j;
    if (ok) {
      if (n == 1) {
        cout << 1 << '\n';
      } else if (n == 2) {
        cout << -1 << '\n';
      } else {
        VVI a(n, VI(n));
        rep(i, n) rep(j, n) {
          int v = j == i || j == (i + 1) % n ? i : j;
          a[i][cj[j].second] = v;
        }
        for (int i : r) {
          rep(j, n) cout << a[i][j] + 1 << " \n"[j + 1 == n];
        }
      }
      continue;
    }
    ok = true;
    rep(j, n - 1) ok &= c[j] == c[j+1];
    if (ok) {
      if (n < 5) {
        cout << -1 << '\n';
        continue;
      }
      int v = c[0];
      VVI a(n, VI(n));
      int nj = 0;
      rep(i, n) {
        rep(j, n) a[i][j] = i;
        if (i != v) {
          if (nj == 0) a[i][nj++] = v, a[i][nj++] = v;
          else a[i][nj++] = v;
        }
      }
      assert(nj == n);
      for (int i : r) {
        rep(j, n) cout << a[i][j] + 1 << " \n"[j + 1 == n];
      }
      continue;
    }
    VI nonex;
    {
      vector<char> seen(n);
      for (int x : c) seen[x] = true;
      rrep(i, n) if (!seen[i]) nonex.emplace_back(i);
    }
    VVI a(n, VI(n));
    for (int l = 0, r = 0; l < n; l = r) {
      int v = cj[l].first;
      static VI js;
      js.clear();
      while (r < n && cj[r].first == v) {
        js.emplace_back(cj[r++].second);
      }
      int sz = js.size();
      int ptr = 0;
      rep(i, n) {
        for (int j : js) a[i][j] = i;
        if (i != v) {
          a[i][js[ptr++]] = v;
          ptr %= sz;
        }
      }
      if (sz == 1 && nonex.size()) {
        int i = nonex.back(); nonex.pop_back();
        a[i][js[0]] = i;
      }
    }
    for (int i : r) {
      rep(j, n) cout << a[i][j] + 1 << " \n"[j + 1 == n];
    }
  }
}
0