結果

問題 No.366 ロボットソート
ユーザー HachimoriHachimori
提出日時 2016-04-29 23:09:29
言語 Python2
(2.7.18)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 6,508 KB
実行使用メモリ 6,132 KB
最終ジャッジ日時 2023-08-28 15:11:44
合計ジャッジ時間 1,783 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,960 KB
testcase_01 AC 11 ms
5,940 KB
testcase_02 AC 11 ms
5,892 KB
testcase_03 AC 11 ms
5,976 KB
testcase_04 AC 10 ms
5,932 KB
testcase_05 AC 11 ms
5,956 KB
testcase_06 AC 10 ms
5,816 KB
testcase_07 AC 11 ms
5,888 KB
testcase_08 AC 11 ms
5,928 KB
testcase_09 AC 11 ms
5,816 KB
testcase_10 AC 11 ms
5,816 KB
testcase_11 AC 13 ms
5,856 KB
testcase_12 AC 14 ms
5,992 KB
testcase_13 AC 12 ms
6,132 KB
testcase_14 AC 13 ms
6,096 KB
testcase_15 AC 11 ms
5,896 KB
testcase_16 AC 11 ms
5,876 KB
testcase_17 AC 13 ms
5,832 KB
testcase_18 AC 17 ms
5,996 KB
testcase_19 AC 15 ms
5,968 KB
testcase_20 AC 11 ms
5,816 KB
testcase_21 AC 40 ms
5,936 KB
testcase_22 AC 96 ms
6,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python
#coding:utf8


def read():
    N, K = map(int, raw_input().split())
    vList = map(int, raw_input().split())
    return K, vList


def calcCost(subList):
    ret = 0
    for i in range(len(subList)):
        for j in range(len(subList) - i - 1):
            if subList[j] > subList[j + 1]:
                subList[j], subList[j + 1] = subList[j + 1], subList[j]
                ret += 1
    return ret


def work((K, vList)):
    
    for i in range(K - 1):
        if i + K >= len(vList) and i + 1 < len(vList) and vList[i] > vList[i + 1]:
            print -1
            return

    ans = 0
    for i in range(min(K, len(vList) - K)):
        ans += calcCost(vList[i::K])
        vList[i::K] = sorted(vList[i::K])
    
    
    print ans if vList == sorted(vList) else -1


if __name__ == "__main__":
    work(read())

0