結果

問題 No.3724 Domination
コンテスト
ユーザー 👑 kencho
提出日時 2026-07-08 15:38:08
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,814 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,141 ms
コンパイル使用メモリ 359,512 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2026-09-19 12:32:07
合計ジャッジ時間 7,156 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % AC * 8
満点 80 % AC * 9 WA * 43
合計 2.5 * 20% = 50 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

bool isPermutation(const vector<int>& values) {
    int n = static_cast<int>(values.size());
    vector<int> seen(n + 1, 0);
    for (int value : values) {
        if (value < 1 || value > n || seen[value] != 0) {
            return false;
        }
        seen[value] = 1;
    }
    return true;
}

bool solveSubCase(int n, const vector<int>& r, const vector<int>& c, vector<vector<int>>& answer) {
    if (!isPermutation(c)) {
        return false;
    }
    if (n == 1) {
        answer.assign(1, vector<int>(1, 1));
        return true;
    }
    if (n == 2) {
        return false;
    }

    vector<int> rowOfValue(n + 1);
    for (int row = 0; row < n; row++) {
        rowOfValue[r[row]] = row;
    }

    answer.assign(n, vector<int>(n));
    for (int row = 0; row < n; row++) {
        fill(answer[row].begin(), answer[row].end(), r[row]);
    }

    for (int col = 0; col < n; col++) {
        int row = (rowOfValue[c[col]] + 1) % n;
        answer[row][col] = c[col];
    }
    return true;
}

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

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> r(n), c(n);
        for (int i = 0; i < n; i++) {
            cin >> r[i];
        }
        for (int i = 0; i < n; i++) {
            cin >> c[i];
        }

        vector<vector<int>> answer;
        if (!solveSubCase(n, r, c, answer)) {
            cout << -1 << '\n';
            continue;
        }

        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                if (col > 0) {
                    cout << ' ';
                }
                cout << answer[row][col];
            }
            cout << '\n';
        }
    }

    return 0;
}
0