結果

問題 No.1838 Modulo Straight
ユーザー 👑 tamatotamato
提出日時 2022-02-11 23:13:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 755 ms / 2,000 ms
コード長 1,758 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 87,752 KB
実行使用メモリ 156,984 KB
最終ジャッジ日時 2023-09-10 06:02:49
合計ジャッジ時間 22,181 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,996 KB
testcase_01 AC 72 ms
71,828 KB
testcase_02 AC 71 ms
71,896 KB
testcase_03 AC 105 ms
78,968 KB
testcase_04 AC 103 ms
78,268 KB
testcase_05 AC 101 ms
78,460 KB
testcase_06 AC 97 ms
78,020 KB
testcase_07 AC 97 ms
77,928 KB
testcase_08 AC 99 ms
78,004 KB
testcase_09 AC 97 ms
78,148 KB
testcase_10 AC 100 ms
78,232 KB
testcase_11 AC 755 ms
156,984 KB
testcase_12 AC 740 ms
156,832 KB
testcase_13 AC 740 ms
156,768 KB
testcase_14 AC 739 ms
156,504 KB
testcase_15 AC 736 ms
156,784 KB
testcase_16 AC 731 ms
152,092 KB
testcase_17 AC 743 ms
151,880 KB
testcase_18 AC 722 ms
152,000 KB
testcase_19 AC 690 ms
148,620 KB
testcase_20 AC 688 ms
148,644 KB
testcase_21 AC 684 ms
148,596 KB
testcase_22 AC 466 ms
138,304 KB
testcase_23 AC 471 ms
143,512 KB
testcase_24 AC 481 ms
137,780 KB
testcase_25 AC 480 ms
136,528 KB
testcase_26 AC 488 ms
136,332 KB
testcase_27 AC 527 ms
141,636 KB
testcase_28 AC 534 ms
141,288 KB
testcase_29 AC 548 ms
141,208 KB
testcase_30 AC 566 ms
141,288 KB
testcase_31 AC 605 ms
143,720 KB
testcase_32 AC 632 ms
143,572 KB
testcase_33 AC 654 ms
152,184 KB
testcase_34 AC 659 ms
152,012 KB
testcase_35 AC 638 ms
147,720 KB
testcase_36 AC 604 ms
146,100 KB
testcase_37 AC 546 ms
143,628 KB
testcase_38 AC 516 ms
141,204 KB
testcase_39 AC 207 ms
100,700 KB
testcase_40 AC 68 ms
71,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

        def add(self, i, x):
            while i <= self.size:
                self.tree[i] += x
                i += i & -i

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

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

    idx = [[-1] * M for _ in range(K)]
    LV = [0] * M
    for i, a in enumerate(A):
        idx[LV[a]][a] = i + 1
        LV[a] += 1

    bit_cur = Bit(N)
    bit_all = Bit(N)
    base = 0
    score_sum = [0] * M
    for lv in range(K):
        I = idx[lv]
        score = [0] * M
        for a in range(M):
            i = I[a]
            base += M * lv - bit_all.sum(i)
            score[0] += a - bit_cur.sum(i)
            bit_cur.add(i, 1)
        for a in range(1, M):
            i = I[a - 1]
            x = bit_cur.sum(i) - 1
            score[a] = score[a - 1] - x + (M - (x + 1))

        # delete
        for a in range(M):
            i = I[a]
            bit_cur.add(i, -1)
            bit_all.add(i, 1)
            score_sum[a] += score[a]
    print(base + min(score_sum))


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