結果

問題 No.366 ロボットソート
ユーザー FromBooskaFromBooska
提出日時 2023-09-04 00:22:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,688 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 75,896 KB
最終ジャッジ日時 2023-09-04 00:22:13
合計ジャッジ時間 3,331 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,084 KB
testcase_01 AC 75 ms
71,232 KB
testcase_02 AC 73 ms
71,472 KB
testcase_03 AC 74 ms
71,388 KB
testcase_04 AC 77 ms
71,276 KB
testcase_05 AC 74 ms
71,292 KB
testcase_06 WA -
testcase_07 AC 77 ms
71,280 KB
testcase_08 WA -
testcase_09 AC 75 ms
71,340 KB
testcase_10 AC 75 ms
71,276 KB
testcase_11 AC 77 ms
71,368 KB
testcase_12 AC 76 ms
71,080 KB
testcase_13 AC 80 ms
75,720 KB
testcase_14 AC 75 ms
71,344 KB
testcase_15 AC 75 ms
71,196 KB
testcase_16 AC 82 ms
75,896 KB
testcase_17 AC 83 ms
75,892 KB
testcase_18 AC 84 ms
75,712 KB
testcase_19 AC 82 ms
75,660 KB
testcase_20 WA -
testcase_21 AC 82 ms
75,660 KB
testcase_22 AC 82 ms
75,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# まず要素を大きさ順で書き換える
# mod Kで要素をグループ分け、そのグループ内なら動かせる
# それぞれのグループにあるべき数字がなければ-1
# あるべき数字があれば、バブルソートの回数は転倒数と同じ
# https://ikatakos.com/pot/programming_algorithm/dynamic_programming/inversion
# その和が答え
# リスト長が1以下なら転倒数数えなくていい

# 転倒数はBinary Indexed Tree BITというデータ構造で高速に求まる
# 数列の最大値以上のサイズのBITを用意、最大値がわかっている必要がある
# https://scrapbox.io/pocala-kyopro/%E8%BB%A2%E5%80%92%E6%95%B0
# https://nagiss.hateblo.jp/entry/2019/07/01/185421

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)

    def __iter__(self):
        psum = 0
        for i in range(self.size):
            csum = self.sum(i+1)
            yield csum - psum
            psum = csum
        raise StopIteration()

    def __str__(self):  # O(nlogn)
        return str(list(self))

    def sum(self, i):
        # [0, i) の要素の総和を返す
        if not (0 <= i <= self.size): raise ValueError("error!")
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        if not (0 <= i < self.size): raise ValueError("error!")
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __getitem__(self, key):
        if not (0 <= key < self.size): raise IndexError("error!")
        return self.sum(key+1) - self.sum(key)

    def __setitem__(self, key, value):
        # 足し算と引き算にはaddを使うべき
        if not (0 <= key < self.size): raise IndexError("error!")
        self.add(key, value - self[key])

def inv_count(LIST):
    bit = Bit(max(LIST)+1)
    count = 0
    for i, a in enumerate(LIST):
        count += i - bit.sum(a+1)
        bit.add(a, 1)
    return count

N, K = map(int, input().split())
A = list(map(int, input().split()))
A_idx = []
for i in range(N):
    A_idx.append((A[i], i))
A_idx.sort()
order = [-1]*N
for j in range(N):
    a, i = A_idx[j]
    order[i] = j+1
    
groups = [[] for i in range(K)]
for i in range(N):
    groups[i%K].append(order[i])
    
test = True
for i in range(K):
    temp = sorted(groups[i])
    for j in range(1, len(temp)):
        if temp[j]-temp[j-1] != K:
            test = False
    
#print(groups)
#print(test)

if test == False:
    ans = -1
else:
    ans = 0
    for i in range(K):
        if len(groups[i]) > 1:
            ans += inv_count(groups[i])
print(ans)
0