結果
| 問題 | No.1854 Limited Bubble Sort |
| コンテスト | |
| ユーザー |
tktk_snsn
|
| 提出日時 | 2022-02-25 21:55:44 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 992 bytes |
| 記録 | |
| コンパイル時間 | 280 ms |
| コンパイル使用メモリ | 82,240 KB |
| 実行使用メモリ | 89,232 KB |
| 最終ジャッジ日時 | 2024-07-03 16:45:02 |
| 合計ジャッジ時間 | 5,863 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 1 WA * 24 |
ソースコード
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
def main():
N = int(input())
P = list(map(lambda x: int(x) - 1, input().split()))
pos = [0] * N
for i, p in enumerate(P):
pos[p] = i
ans = []
def swap(x, y):
if x > y:
x, y = y, x
P[x], P[y] = P[y], P[x]
pos[P[x]] = x
pos[P[y]] = y
ans.append(x + 1)
def F(L, R):
if L == R:
return True
for x in range(L, R)[::-1]:
if pos[x] < L or pos[x] >= R:
return False
while pos[x] < x:
i = pos[x]
swap(i, i+1)
return True
L = 0
for R in range(N):
if P[R] == R:
if not F(L, R):
print(-1)
return
L = R + 1
if not F(L, N):
print(-1)
return
print(len(ans))
print(*ans)
return
T = int(input())
for _ in range(T):
main()
tktk_snsn