結果

問題 No.1838 Modulo Straight
ユーザー 👑 hitonanodehitonanode
提出日時 2021-12-19 17:36:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 635 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 185,836 KB
最終ジャッジ日時 2023-10-13 18:55:59
合計ジャッジ時間 15,995 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
71,192 KB
testcase_01 AC 61 ms
71,184 KB
testcase_02 AC 61 ms
71,484 KB
testcase_03 AC 87 ms
78,212 KB
testcase_04 AC 77 ms
77,516 KB
testcase_05 AC 75 ms
76,924 KB
testcase_06 AC 73 ms
76,828 KB
testcase_07 AC 75 ms
77,408 KB
testcase_08 AC 74 ms
76,844 KB
testcase_09 AC 76 ms
76,996 KB
testcase_10 AC 77 ms
77,600 KB
testcase_11 AC 468 ms
185,660 KB
testcase_12 AC 635 ms
185,700 KB
testcase_13 AC 469 ms
185,836 KB
testcase_14 AC 480 ms
185,696 KB
testcase_15 AC 467 ms
185,592 KB
testcase_16 AC 424 ms
159,528 KB
testcase_17 AC 430 ms
159,304 KB
testcase_18 AC 438 ms
159,484 KB
testcase_19 AC 373 ms
154,052 KB
testcase_20 AC 393 ms
153,972 KB
testcase_21 AC 386 ms
153,968 KB
testcase_22 AC 310 ms
167,160 KB
testcase_23 AC 300 ms
165,108 KB
testcase_24 AC 286 ms
148,000 KB
testcase_25 AC 275 ms
129,888 KB
testcase_26 AC 280 ms
130,200 KB
testcase_27 AC 284 ms
133,956 KB
testcase_28 AC 289 ms
136,472 KB
testcase_29 AC 295 ms
137,880 KB
testcase_30 AC 312 ms
145,028 KB
testcase_31 AC 332 ms
148,428 KB
testcase_32 AC 328 ms
151,004 KB
testcase_33 AC 409 ms
158,100 KB
testcase_34 AC 415 ms
158,176 KB
testcase_35 AC 343 ms
156,616 KB
testcase_36 AC 329 ms
151,700 KB
testcase_37 AC 312 ms
147,608 KB
testcase_38 AC 292 ms
137,564 KB
testcase_39 AC 156 ms
103,144 KB
testcase_40 AC 59 ms
71,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://github.com/hitonanode/Polaris/blob/main/fenwick.py
class Fenwick:
    """1-indexed Fenwick tree (Binary indexed tree)
    """

    def __init__(self, N: int) -> None:
        self.sz = N + 1
        self.v = [0] * (self.sz)

    def add(self, pos: int, val: int) -> None:
        while pos > 0 and pos < self.sz:
            self.v[pos] += val
            pos += pos & -pos

    def sum(self, pos: int) -> int:
        ret = 0
        while pos:
            ret += self.v[pos]
            pos -= pos & -pos
        return ret


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

ord = [0] * N
freq = [0] * M
v2i = [list() for _ in range(M)]

for i, a in enumerate(A):
    ord[i] = a + freq[a] * M
    v2i[a].append(i)
    freq[a] += 1

prvs = [list() for _ in range(K)]
prvs_ord = [0] * N

current = 0
bit = Fenwick(N)
for i, ord_i in enumerate(ord):
    current += bit.sum(N) - bit.sum(ord_i)
    bit.add(ord_i + 1, 1)
    o = ord_i // M
    prvs_ord[i] = len(prvs[o])
    prvs[o].append(i)

ret = current

for Is in v2i:
    for t, i in enumerate(Is):
        nlo = prvs_ord[i]
        nhi = M - 1 - nlo
        current += nhi - nlo

    ret = min(ret, current)

print(ret)
0