結果

問題 No.366 ロボットソート
ユーザー zombietanzombietan
提出日時 2016-06-07 08:08:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 8,412 KB
最終ジャッジ日時 2023-08-28 15:17:25
合計ジャッジ時間 1,567 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,316 KB
testcase_01 AC 16 ms
8,164 KB
testcase_02 AC 17 ms
8,240 KB
testcase_03 AC 16 ms
8,244 KB
testcase_04 AC 16 ms
8,244 KB
testcase_05 AC 15 ms
8,224 KB
testcase_06 AC 16 ms
8,312 KB
testcase_07 AC 15 ms
8,268 KB
testcase_08 AC 16 ms
8,288 KB
testcase_09 AC 16 ms
8,264 KB
testcase_10 AC 15 ms
8,332 KB
testcase_11 AC 17 ms
8,352 KB
testcase_12 AC 18 ms
8,384 KB
testcase_13 AC 16 ms
8,280 KB
testcase_14 AC 17 ms
8,328 KB
testcase_15 AC 16 ms
8,272 KB
testcase_16 AC 17 ms
8,360 KB
testcase_17 AC 17 ms
8,232 KB
testcase_18 AC 21 ms
8,344 KB
testcase_19 AC 19 ms
8,276 KB
testcase_20 AC 16 ms
8,200 KB
testcase_21 AC 49 ms
8,340 KB
testcase_22 AC 112 ms
8,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
N, K = map(int, input().split())
block = list(map(int, input().split()))
count = 0

def sort(b):
    global count
    k = len(b) - 1
    for i in range(k):
        for j in range(k, i, -1):
            if b[j - 1] > b[j]:
                count += 1
                temp = b[j]
                b[j] = b[j - 1]
                b[j - 1] = temp
    return b

def is_sorted(result):
    if len(result) == 1:
        return True
    m = 0
    for e in result:
        if e < m:
            return False
        m = e
    return True

if N <= K:
    if is_sorted(block):
        print(0)
    else:
        print(-1)
else:
    div = []
    for start in range(K):
        itr = itertools.islice(block, start, None, K)
        div.append(list(itr))
        
    sort_div = []
    for d in div:
        sort_div.append(sort(d))
    
    done_swap = [list(z) for z in itertools.zip_longest(*sort_div)]
    done_swap = [n for n in sum(done_swap,[]) if n != None]
    if is_sorted(done_swap):
        print(count)
    else:
        print(-1)
0