結果

問題 No.366 ロボットソート
ユーザー tookunn_1213tookunn_1213
提出日時 2016-04-29 23:36:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,637 ms / 2,000 ms
コード長 564 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-10-04 19:15:29
合計ジャッジ時間 3,313 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

def check(a,b,N):
	for i in range(N):
		if a[i] not in b[i % K]:
			return False
	return True
N,K = map(int,input().split())
a = [int(i) for i in input().split()]
b = [set([]) for i in range(K)]
c = sorted([i for i in a])
for i in range(N):
	b[i % K].add(c[i])
if not check(a,b,N):
	print(-1)
else:
	cnt = 0
	while True:
		flag = True
		for i in range(N - 1):
			if a[i] > a[i + 1]:
				flag = False
		if flag:
			break
		for i in range(N):
			j = i + K
			while j < N:
				if a[j - K] > a[j]:
					a[j],a[j - K] = a[j - K],a[j]
					cnt+=1
				j += K
	print (cnt)
0