結果

問題 No.366 ロボットソート
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-29 22:53:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 589 ms / 2,000 ms
コード長 730 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 86,948 KB
実行使用メモリ 76,768 KB
最終ジャッジ日時 2023-08-28 15:11:28
合計ジャッジ時間 3,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,160 KB
testcase_01 AC 69 ms
71,264 KB
testcase_02 AC 71 ms
71,096 KB
testcase_03 AC 70 ms
71,472 KB
testcase_04 AC 70 ms
71,304 KB
testcase_05 AC 70 ms
71,172 KB
testcase_06 AC 70 ms
71,300 KB
testcase_07 AC 70 ms
71,396 KB
testcase_08 AC 69 ms
71,160 KB
testcase_09 AC 70 ms
71,156 KB
testcase_10 AC 75 ms
75,460 KB
testcase_11 AC 96 ms
75,832 KB
testcase_12 AC 84 ms
76,064 KB
testcase_13 AC 73 ms
75,644 KB
testcase_14 AC 75 ms
75,472 KB
testcase_15 AC 74 ms
75,520 KB
testcase_16 AC 74 ms
75,932 KB
testcase_17 AC 78 ms
75,660 KB
testcase_18 AC 93 ms
75,984 KB
testcase_19 AC 83 ms
76,076 KB
testcase_20 AC 68 ms
70,992 KB
testcase_21 AC 71 ms
71,356 KB
testcase_22 AC 589 ms
76,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

IMPOSSIBLE = -1


def in_place_swap(xs, i, k):
    xs[i], xs[i + k] = xs[i + k], xs[i]


def is_meaningful_swap(xs, i, k):
    return xs[i] >= xs[i + k]


def min_num_of_operations(n, k, xs):
    counter = 0
    while True:
        for i in range(len(xs) - k):
            if is_meaningful_swap(xs, i, k):
                in_place_swap(xs, i, k)
                counter += 1
                break
        else:
            if sorted(xs) == list(xs):
                return counter
            else:
                return IMPOSSIBLE


def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    print(min_num_of_operations(n, k, xs))


if __name__ == '__main__':
    main()
0