結果

問題 No.1838 Modulo Straight
ユーザー 👑 hitonanodehitonanode
提出日時 2021-12-19 17:36:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,273 bytes
コンパイル時間 896 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 134,980 KB
最終ジャッジ日時 2023-10-13 18:55:34
合計ジャッジ時間 5,766 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,592 KB
testcase_01 AC 17 ms
8,240 KB
testcase_02 AC 16 ms
8,296 KB
testcase_03 AC 30 ms
8,816 KB
testcase_04 AC 30 ms
8,808 KB
testcase_05 AC 29 ms
8,688 KB
testcase_06 AC 29 ms
8,776 KB
testcase_07 AC 29 ms
8,692 KB
testcase_08 AC 28 ms
8,492 KB
testcase_09 AC 30 ms
8,856 KB
testcase_10 AC 33 ms
9,036 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

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