結果

問題 No.1306 Exactly 2 Digits
ユーザー PachicobuePachicobue
提出日時 2020-12-03 19:55:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,708 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 211,152 KB
実行使用メモリ 36,160 KB
最終ジャッジ日時 2023-09-24 08:27:51
合計ジャッジ時間 9,303 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
testcase_101 -- -
testcase_102 -- -
testcase_103 -- -
testcase_104 -- -
testcase_105 -- -
testcase_106 -- -
testcase_107 -- -
testcase_108 -- -
testcase_109 -- -
testcase_110 -- -
testcase_111 -- -
testcase_112 -- -
testcase_113 -- -
testcase_114 -- -
testcase_115 -- -
testcase_116 -- -
testcase_117 -- -
testcase_118 -- -
testcase_119 -- -
testcase_120 -- -
testcase_121 -- -
testcase_122 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
int main()
{
    int N;
    std::cin >> N;
    if (N == 2) {
        while (true) {}
    }
    const int L = N * N - N;
    using pii   = std::pair<int, int>;
    auto ask    = [&](const int i, const int j) {
        std::cout << "? " << i + 1 << " " << j + 1 << std::endl;
        int p, q;
        std::cin >> p >> q;
        return std::make_pair(p, q);
    };
    auto fin = [&](const std::vector<int>& ans) {
        std::cout << "!";
        for (const int e : ans) { std::cout << " " << e; }
        std::cout << std::endl;
    };
    auto diff = [&](const int m, const int n) {  // m-n
        const auto [p, q] = std::minmax(m / N - n / N, m % N - n % N);
        return std::make_pair(p, q);
    };

    std::vector<pii> ds(L, {0, 0});  // ds[i] = as[i]-as[0]
    for (int i = 1; i < L; i++) { ds[i] = ask(i, 0); }

    std::vector<int> as(L, -1);
    const int mi = std::min_element(ds.begin(), ds.end()) - ds.begin();
    const int Mi = std::max_element(ds.begin(), ds.end()) - ds.begin();
    as[mi] = N, as[Mi] = N * N - 1;

    auto getcand = [&](const int n, const pii d) {  // n, n-c
        auto [p, q] = d;
        std::vector<int> ans;
        const int a = n / N, b = n % N;
        if (1 <= a - p and a - p < N and 0 <= b - q and b - q < N) { ans.push_back((a - p) * N + (b - q)); }
        if (p != q) {
            std::swap(p, q);
            if (1 <= a - p and a - p < N and 0 <= b - q and b - q < N) { ans.push_back((a - p) * N + (b - q)); }
        }
        return ans;
    };
    auto decide = [&](const int m, const pii& md, const int n, const pii& nd) {  // m,m-c,n,n-c
        std::vector<int> ans;
        const auto cs = getcand(m, md);
        for (const int c : cs) {
            if (diff(n, c) == nd) { ans.push_back(c); }
        }
        return ans[0];
    };

    as[0]   = decide(N, ds[mi], N * N - 1, ds[Mi]);
    int ref = Mi;
    {
        const int p = as[0] / N, q = as[0] % N;
        if (p == q) { ref = mi; }
    }

    std::vector<bool> used(N * N, false);
    used[N] = used[as[0]] = used[N * N - 1] = true;
    for (int i = 1; i < L; i++) {
        const auto cs = getcand(as[0], pii{-ds[i].second, -ds[i].first});
        if (cs.size() == 1) {
            as[i] = cs[0];
        } else if (cs.size() == 2) {
            if (used[cs[0]]) {
                as[i] = cs[1];
            } else if (used[cs[1]]) {
                as[i] = cs[0];
            } else {
                const auto d = ask(ref, i);
                as[i]        = decide(as[0], pii{-ds[i].second, -ds[i].first}, as[ref], d);
            }
        } else {
            while (true) {}
        }
        used[as[i]] = true;
    }
    fin(as);
    return 0;
}
0