結果

問題 No.366 ロボットソート
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2024-04-06 20:46:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 70,556 KB
最終ジャッジ日時 2024-04-06 20:46:34
合計ジャッジ時間 2,812 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,608 KB
testcase_01 AC 45 ms
55,608 KB
testcase_02 AC 45 ms
55,608 KB
testcase_03 AC 49 ms
55,608 KB
testcase_04 AC 45 ms
55,608 KB
testcase_05 AC 48 ms
55,608 KB
testcase_06 AC 44 ms
55,608 KB
testcase_07 AC 57 ms
55,608 KB
testcase_08 AC 45 ms
55,608 KB
testcase_09 AC 44 ms
55,604 KB
testcase_10 AC 45 ms
55,608 KB
testcase_11 AC 46 ms
57,704 KB
testcase_12 AC 47 ms
57,704 KB
testcase_13 AC 47 ms
57,704 KB
testcase_14 AC 47 ms
57,704 KB
testcase_15 AC 46 ms
57,704 KB
testcase_16 AC 52 ms
63,676 KB
testcase_17 AC 56 ms
65,876 KB
testcase_18 AC 65 ms
70,556 KB
testcase_19 AC 58 ms
65,896 KB
testcase_20 AC 46 ms
55,608 KB
testcase_21 AC 58 ms
65,868 KB
testcase_22 AC 57 ms
65,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline


def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1=0
        i2=0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt


N,K = map(int,input().split())
A = list(map(int,input().split()))

B = [[] for _ in range(K)]
C = [[] for _ in range(K)]

for i in range(N):
    idx = i%K
    B[idx].append(A[i])
    C[idx].append(A[i])

for i in range(K):
    C[i].sort()
T = [0]*N
for i in range(N):
    p,r = divmod(i,K)
    T[i]=C[r][p]
if any(T[i]>=T[i+1] for i in range(N-1)):
    print(-1)
    exit()
ans = 0
for b in B:
    ans += mergecount(b)
print(ans)

0