結果
| 問題 | No.3529 2p Teleportations |
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2026-06-22 15:11:09 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,652 bytes |
| 記録 | |
| コンパイル時間 | 299 ms |
| コンパイル使用メモリ | 85,384 KB |
| 実行使用メモリ | 54,172 KB |
| 最終ジャッジ日時 | 2026-06-22 15:11:54 |
| 合計ジャッジ時間 | 5,109 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | TLE * 1 -- * 48 |
ソースコード
import sys
input = sys.stdin.readline
T=int(input())
for tests in range(T):
N,P=list(map(int,input().split()))
A=list(map(int,input().split()))
for i in range(N):
A[i]-=1
A_INV=[-1]*N
for i in range(N):
A_INV[A[i]]=i
USE=[0]*N
ANS=[-1]*N
LIST=[[] for i in range(N+1)]
for i in range(N):
if USE[i]==0:
now=i
L=[now]
while True:
USE[now]=1
now=A[now]
if now==L[0]:
break
L.append(now)
LIST[len(L)].append(L)
#print(LIST)
for i in range(1,N+1):
if i==1:
for L in LIST[i]:
ANS[L[0]]=L[0]
elif i%2==0:
for j in range(0,len(LIST[i]),2):
if j+1>=len(LIST[i]):
break
c=-1
L=LIST[i][j]+LIST[i][j+1]
#print(L)
k=2*P%len(L)
if k==0:
for x in L:
ANS[x]=x
continue
X=[-1]*len(L)
X[0]=A[L[0]]
X[k]=L[0]
ind=k
while True:
now=X[ind]
c=A_INV[now]
if X[(ind+k)%len(L)]==-1:
X[(ind+k)%len(L)]=c
ind=(ind+k)%len(L)
else:
break
#print(X)
X[1]=A[L[i]]
X[1+k]=L[i]
ind=1+k
#print(i,k,L,X)
while True:
now=X[ind]
c=A_INV[now]
if X[(ind+k)%len(L)]==-1:
X[(ind+k)%len(L)]=c
ind=(ind+k)%len(L)
else:
break
#print("!",X)
for ix in range(len(X)):
ANS[X[ix]]=X[(ix+1)%len(X)]
#print(ANS)
if len(LIST[i])%2==1:
#print("!!!")
L=LIST[i][-1]
c=L.pop()
k=2*P%len(L)
if k==0:
for x in L:
ANS[x]=x
continue
X=[-1]*len(L)
X[0]=A[L[0]]
X[k]=L[0]
ind=k
while True:
now=X[ind]
c=A_INV[now]
if X[(ind+k)%len(L)]==-1:
X[(ind+k)%len(L)]=c
ind=(ind+k)%len(L)
else:
break
#print("?",X)
for i in range(len(X)):
ANS[X[i]]=X[(i+1)%len(X)]
else:
c=-1
if len(L)%2==0:
c=L.pop()
k=2*P%len(L)
if k==0:
for x in L:
ANS[x]=x
continue
X=[-1]*len(L)
X[0]=A[L[0]]
X[k]=L[0]
ind=k
while True:
now=X[ind]
c=A_INV[now]
if X[(ind+k)%len(L)]==-1:
X[(ind+k)%len(L)]=c
ind=(ind+k)%len(L)
else:
break
for i in range(len(X)):
ANS[X[i]]=X[(i+1)%len(X)]
for i in range(N):
ANS[i]+=1
ANS[i]=max(ANS[i],1)
print(*ANS)
titia