結果
問題 | No.1306 Exactly 2 Digits |
ユーザー |
|
提出日時 | 2020-12-03 19:47:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,605 bytes |
コンパイル時間 | 2,517 ms |
コンパイル使用メモリ | 204,648 KB |
最終ジャッジ日時 | 2025-01-16 14:32:44 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | RE * 123 |
ソースコード
#include <bits/stdc++.h> int main() { int N; std::cin >> N; 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) % 2 == 1) { 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 (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); } } used[as[i]] = true; } fin(as); return 0; }