結果
| 問題 | No.366 ロボットソート |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-10 17:01:29 |
| 言語 | Python3 (3.14.3 + numpy 2.4.2 + scipy 1.17.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 828 bytes |
| 記録 | |
| コンパイル時間 | 634 ms |
| コンパイル使用メモリ | 20,812 KB |
| 実行使用メモリ | 15,484 KB |
| 最終ジャッジ日時 | 2026-02-10 17:01:35 |
| 合計ジャッジ時間 | 5,124 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 8 |
ソースコード
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = sorted(a)
ans = 0
for i in range(k):
group_a = []
group_b = []
j = i
while j < n:
group_a.append(a[j])
group_b.append(b[j])
j += k
if sorted(group_a) != sorted(group_b):
print(-1)
sys.exit()
# đếm số swap tối thiểu
pos = {v: idx for idx, v in enumerate(group_b)}
visited = [False] * len(group_a)
for j in range(len(group_a)):
if visited[j] or pos[group_a[j]] == j:
continue
cycle = 0
x = j
while not visited[x]:
visited[x] = True
x = pos[group_a[x]]
cycle += 1
if cycle > 1:
ans += cycle - 1
print(ans)