結果

問題 No.3131 Twin Slide Puzzles
ユーザー Kude
提出日時 2025-04-25 23:07:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,241 bytes
コンパイル時間 4,026 ms
コンパイル使用メモリ 312,952 KB
実行使用メモリ 69,460 KB
最終ジャッジ日時 2025-04-25 23:07:34
合計ジャッジ時間 16,879 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other AC * 12 WA * 22 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#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>;

void answer(int n, const VI& p, const VI& q) {
  cout << "Yes\n";
  rep(i, n) rep(j, n) cout << p[i * n + j] << " \n"[j + 1 == n];
  rep(i, n) rep(j, n) cout << q[i * n + j] << " \n"[j + 1 == n];
}

std::default_random_engine gen(std::chrono::system_clock::now().time_since_epoch().count());
using dist_type = std::uniform_int_distribution<>;
int RI(int L, int R) { return dist_type(L, R - 1)(gen); }

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int n2 = n * n;
  VI a(n2);
  rep(i, n2) cin >> a[i];
  VI x(n2);
  iota(all(x), 0);
  if (n <= 3) {
    map<ll, VI> memo;
    do {
      ll v = 0;
      rep(i, n2) v += (ll)a[i] * x[i];
      auto [it, inserted] = memo.emplace(v, x);
      if (!inserted) {
        answer(n, it->second, x);
        return 0;
      }
    } while (next_permutation(all(x)));
    cout << "No\n";
    return 0;
  }
  unordered_map<ll, int> memo;
  vector<P> hist;
  ll now = 0;
  rep(i, n2) now += (ll)a[i] * x[i];
  memo[now] = ssize(hist);
  while (true) {
    int p1 = RI(0, n2), p2 = RI(0, n2);
    if (p1 == p2) continue;
    int dx = x[p2] - x[p1];
    now += (ll)a[p1] * dx - (ll)a[p2] * dx;
    swap(x[p1], x[p2]);
    hist.emplace_back(p1, p2);
    auto [it, inserted] = memo.emplace(now, ssize(hist));
    if (!inserted) {
      VI x1 = x;
      while (ssize(hist) != it->second) {
        auto [p1, p2] = hist.back();
        hist.pop_back();
        swap(x[p1], x[p2]);
      }
      answer(n, x, x1);
      return 0;
    }
  }
}
0