結果
| 問題 |
No.1838 Modulo Straight
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-12-19 17:36:40 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 625 ms / 2,000 ms |
| コード長 | 1,273 bytes |
| コンパイル時間 | 1,052 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 184,968 KB |
| 最終ジャッジ日時 | 2024-09-15 14:42:05 |
| 合計ジャッジ時間 | 16,140 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 38 |
ソースコード
# 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)
hitonanode