結果

問題 No.1306 Exactly 2 Digits
ユーザー mkawa2
提出日時 2020-12-04 13:18:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 28,488 KB
平均クエリ数 1245.39
最終ジャッジ日時 2024-07-17 09:28:18
合計ジャッジ時間 19,865 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 123
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

# aa = [12, 13, 11, 6, 15, 5, 7, 9, 14, 10, 8, 4]
# aa = [3, 5, 4, 8, 7, 6]
# def ask(i, j):
#     a = aa[i]//n-aa[j]//n
#     b = aa[i]%n-aa[j]%n
#     return min(a, b), max(a, b)

def ask(i, j):
    print("?", i+1, j+1, flush=True)
    p, q = map(int, input().split())
    return p, q

n = int(input())
# aa = list(map(int, input().split()))
m = n**2-n
pqi = defaultdict(list)
for i in range(m-1):
    p, q = ask(i, m-1)
    pqi[p, q].append(i)
# print(pqi)

ans = [0]*m
(p0, q0), mni = min(pqi.items(), key=lambda x: sum(x[0]))
(p1, q1), mxi = max(pqi.items(), key=lambda x: sum(x[0]))
# print(p0, q0, mni)
# print(p1, q1, mxi)

if (p0, q0) == (0, 1):
    a = 1
    b = 0
    ans[mxi[0]] = n**2-1
elif (p1, q1) == (-1, 0):
    a = n-1
    b = n-1
    ans[mni[0]] = n
else:
    can = [(n-1-p1, n-1-q1), (n-1-q1, n-1-p1)]
    if (1-p0, -q0) in can:
        a, b = 1-p0, -q0
    else:
        a, b = 1-q0, -p0
    ans[mxi[0]] = n**2-1
    ans[mni[0]] = n

ans[-1] = a*n+b
# print(ans)

for (p, q), ii in pqi.items():
    if len(ii) == 1:
        i = ii[0]
        if ans[i]: continue
        x0, y0 = a+p, b+q
        x1, y1 = a+q, b+p
        if 1 <= x0 < n and 0 <= y0 < n:
            ans[i] = x0*n+y0
        else:
            ans[i] = x1*n+y1

(p2, q2), [ci] = max(pqi.items(), key=lambda x: x[0][1]-x[0][0])
# print(p2, q2, ci)

c, d = divmod(ans[ci], n)
for (ap, aq), ii in pqi.items():
    if len(ii) == 2:
        i0, i1 = ii
        cp, cq = ask(i0, ci)
        x0, y0 = c+cp, d+cq
        x1, y1 = c+cq, d+cp
        if 1 <= x0 < n and 0 <= y0 < n:
            ans[i0] = x0*n+y0
        else:
            ans[i0] = x1*n+y1

        x0, y0 = a+ap, b+aq
        x1, y1 = a+aq, b+ap
        if ans[i0] == x0*n+y0: ans[i1] = x1*n+y1
        else: ans[i1] = x0*n+y0

print("!", end=" ")
print(*ans)
0