結果

問題 No.1838 Modulo Straight
ユーザー hitonanode
提出日時 2021-12-19 17:36:25
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,273 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 138,508 KB
最終ジャッジ日時 2024-09-15 14:41:43
合計ジャッジ時間 5,388 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8 TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

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