結果

問題 No.3131 Twin Slide Puzzles
ユーザー Iroha_3856
提出日時 2025-04-25 21:01:35
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 269 ms / 4,000 ms
コード長 2,124 bytes
コンパイル時間 4,302 ms
コンパイル使用メモリ 296,692 KB
実行使用メモリ 25,984 KB
最終ジャッジ日時 2025-06-20 02:37:06
合計ジャッジ時間 20,888 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++)
#define ll long long
#define siz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()

int main() {
    int N; cin >> N;
    vector<ll> A(N*N);
    rep(i, 0, N*N) cin >> A[i];
    auto score = [&](vector<int> P) {
        ll ret = 0;
        rep(i, 0, siz(P)) {
            ret += A[i] * P[i];
        }
        return ret;
    };
    auto isok = [&](vector<int> P) {
        int x = 0, y = 0;
        rep(i, 0, siz(P)) {
            if (P[i] == 0) {
                x = i/N+i%N;
            }
        }
        rep(i, 0, siz(P)) rep(j, i+1, siz(P)) {
            if (P[i] > P[j]) y++;
        }
        return x%2 == y%2;
    };
    auto print = [&](vector<int> P) {
        vector<vector<int>> ans(N, vector<int>(N));
        rep(i, 0, N) rep(j, 0, N) ans[i][j] = i*N+j;
        rep(i, 0, siz(P)) ans[i/N][i%N] = P[i];
        rep(i, 0, N) {
            rep(j, 0, N) cout << ans[i][j] << " ";
            cout << endl;
        }
    };
    if (N <= 3) {
        vector<int> P(N*N);
        iota(all(P), 0);
        map<int, vector<int>> mp;
        do {
            if (isok(P)) {
                int sc = score(P);
                if (mp.contains(sc) and mp[sc] != P) {
                    cout << "Yes" << endl;
                    print(mp[sc]);
                    print(P);
                    return 0;
                }
                mp[sc] = P;
            }
        }while(next_permutation(all(P)));
        cout << "No" << endl;
    }
    else {
        map<int, vector<int>> mp;
        vector<int> P(16);
        iota(all(P), 0);
        random_device seed;
        mt19937 rnd(seed());
        while(true) {
            shuffle(all(P), rnd);
            if (isok(P)) {
                int sc = score(P);
                if (mp.contains(sc) and mp[sc] != P) {
                    cout << "Yes" << endl;
                    print(mp[sc]);
                    print(P);
                    return 0;
                }
                mp[sc] = P;
            }
        }
    }
}
0