結果

問題 No.1306 Exactly 2 Digits
ユーザー shotoyooshotoyoo
提出日時 2020-12-03 12:27:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 307 ms / 2,000 ms
コード長 3,413 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,204 KB
実行使用メモリ 96,164 KB
平均クエリ数 1425.22
最終ジャッジ日時 2024-07-17 08:41:06
合計ジャッジ時間 26,949 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 123
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

DEBUG = False
def main(n,a):
#     print(a)
    N = 0
    def _sub(i,j):
        p = a[i-1]//n - a[j-1]//n
        q = a[i-1]%n - a[j-1]%n
        if p>q:
            p,q = q,p
        return p,q
    import sys
    def query(i,j):
        nonlocal N
        N += 1
        print("? {0} {1}".format(i, j))
        sys.stdout.flush()
        if DEBUG:
            return _sub(i,j)
        else:
            return tuple(map(int, input().split()))
    if n==2:
        t = query(1,2)
        if t==(-1,0):
            print("! 2 3")
        else:
            print("! 3 2")
        return
    m = n*n-n
    ts = [query(1,i) for i in range(2,m+1)]
    from collections import defaultdict
    c = defaultdict(int)
    for t in ts:
        c[t[0]] += 1
        c[t[1]] += 1
    kv = list(c.items())
    kv.sort()
    ys = []
    if kv[0][1]==n-1:
        val = kv[0][0]
        y = kv[0][0] + n - 1
        for item in kv:
            if item[1]==2*n-1 or (item[1]==2*n-3 and item[0]==0):
                break
            else:
                ys.append(item[0])
        x = item[0] + n - 1
    #     print(x,item)
    else:
    #     assert kv[-1][1]==n-1
        y = kv[-1][0]
        val = kv[-1][0]
        for item in kv[::-1]:
            if item[1]==2*n-1 or (item[1]==2*n-3 and item[0]==0):
                break
            else:
                ys.append(item[0])
        x = item[0] + 1
    ans = [None]*m
    ans[0] = (x,y)
    xy2i = defaultdict(list)
#     print(val, ys, ts)
    for i,t in enumerate(ts):
        xy2i[min(t),max(t)].append(i)
        if val in t:
            if t[0]==val:
                t = (t[1], t[0])
            ans[i+1] = (x-t[0], y-t[1])
            if x-t[0]==n-1 and y-t[1]==0:
                index = i
                large = True
            elif x-t[0]==1 and y-t[1]==n-1:
                index = i
                large = False
        else:
            for vv in ys[1:]:
                if vv in t:
                    if t[0]==vv:
                        t = (t[1],t[0])
                    ans[i+1] = (x-t[0], y-t[1])
    for i,t in enumerate(ts):
        if ans[i+1] is None:
            tt = query(index+2, i+2)
            if large:
                tt = (tt[1],tt[0])
                ans[i+1] = (n-1-tt[0], -tt[1])
            else:
                ans[i+1] = (1-tt[0], n-1-tt[1])
            xx,yy = x - ans[i+1][0], y - ans[i+1][1]
            if xx<=yy:
                for ii in xy2i[xx,yy]:
                    if i==ii:
                        continue
                    if ans[ii+1] is None:
                        ans[ii+1] = (x-yy, y-xx)
    #                     print(ans[ii+1], ii+1)
            else:
                for ii in xy2i[yy,xx]:
                    if i==ii:
                        continue
                    if ans[ii+1] is None:
                        ans[ii+1] = (x-yy, y-xx)
    #                     print(ans[ii+1], ii+1)
    l = [item[0]*n+item[1] for item in ans]
    print("! "+ " ".join(map(str, l)))
#     print(xy2i)
    assert N<=3*n*(n-1)//2
# a = [3,5,4,8,7,6]
from itertools import permutations
from random import shuffle
import random
# random.seed(0)
n = int(input())
l = list(range(n, n*n))
shuffle(l)
main(n,l)
# for a in permutations(range(n, n*n)):
#     main(n,list(a))
0