結果

問題 No.1838 Modulo Straight
ユーザー 👑 rin204rin204
提出日時 2022-02-12 00:01:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,356 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 87,388 KB
実行使用メモリ 227,428 KB
最終ジャッジ日時 2023-09-10 07:11:27
合計ジャッジ時間 26,729 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,404 KB
testcase_01 AC 71 ms
71,476 KB
testcase_02 AC 72 ms
71,588 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 258 ms
115,076 KB
testcase_40 AC 71 ms
71,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

m, k = map(int, input().split())
A = list(map(int, input().split()))

ans = [0] * m
cnt = [0] * m
lst = []
for a in A:
    c = cnt[a] * m + a
    lst.append(c)
    cnt[a] += 1
bit = Bit(m * k)
ans_base = 0
for a in lst:
    ans_base += bit.range_sum(a, m * k)
    bit.add(a, 1)

cnt = [0] * m
for i in range(k):
    lst = []
    for j in range(i * m, i * m + m):
        a = A[j]
        c = cnt[a] * m + a
        lst.append(c)
        cnt[a] += 1
    dic = {l:i for i, l in enumerate(sorted(lst))}
    bit = Bit(m)
    tot = 0
    for a in lst:
        a = dic[a]
        tot += bit.range_sum(a, m)
        bit.add(a, 1)
    base = tot
    i = 1
    for a in lst[:-1]:
        a = dic[a]
        tot -= a
        tot += m - a - 1
        ans[i] += tot - base
        i += 1

print(min(ans) + ans_base)
    
    
0