結果

問題 No.2740 Old Maid
ユーザー tassei903
提出日時 2024-04-12 00:08:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 697 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 114,780 KB
最終ジャッジ日時 2024-10-08 12:14:58
合計ジャッジ時間 10,119 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
p = [x-1 for x in list(map(int, input().split()))]
invp = [-1]*n
for i in range(n):
    invp[p[i]] = i


next = [-1] * n
prev = [-1] * n
for i in range(n):
    next[i] = (i+1)%n
    prev[i] = (i-1)%n

def remove(x):
    prev[next[x]] = prev[x]
    next[prev[x]] = next[x]


removed = [0] * n
removed[p[-1]] = 1

last = n-1
ans = []
x = 0
for i in range(n//2):
    while removed[x]:
        x += 1
    
    ans.append(x+1)
    y = p[next[invp[x]]]
    ans.append(y+1)
    remove(invp[x])
    remove(invp[y])
    removed[x] = 1
    removed[y] = 1
    if invp[y] == last and i < n//2-1:
        while removed[p[last]]:
            last -= 1
        removed[p[last]] = 1

print(*ans)
0