結果

問題 No.1604 Swap Sort:ONE
ユーザー player
提出日時 2023-12-20 16:11:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 567 ms / 2,000 ms
コード長 557 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 78,628 KB
最終ジャッジ日時 2024-09-27 09:52:31
合計ジャッジ時間 9,266 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *

n = int(input())
p = list(map(int, input().split()))

pp = []
for i in range(1, n + 1):
    pp.append(i)
d = defaultdict(int)
for i in range(n):
    d[p[i]] = i  # 要素p[i]はどのインデックスにあるか

cnt = 0

for i in range(1, n + 1):
    if p == pp:
        print(cnt)
        exit()
    if d[i] == i - 1:
        continue
    for j in range(p[i - 1], i , -1):
        d[j], d[j - 1] = d[j - 1], d[j]
        cnt += 1
    tmp = [None] * n
    for j in range(1, n + 1):
        tmp[d[j]] = j
    p = tmp

print(cnt)
0