結果
| 問題 |
No.2382 Amidakuji M
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-16 01:56:36 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,891 bytes |
| コンパイル時間 | 420 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 206,392 KB |
| 最終ジャッジ日時 | 2024-08-16 01:56:43 |
| 合計ジャッジ時間 | 5,091 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 WA * 6 |
ソースコード
import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
sys.set_int_max_str_digits(10 ** 6)
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]
def compress(S):
""" 座標圧縮 """
S = set(S)
zipped, unzipped = {}, {}
for i, a in enumerate(sorted(S), start=1):
zipped[a] = i
unzipped[i] = a
return zipped, unzipped
class BIT():
def __init__(self, n):
"""
1-index
sum -> i番目までの和
add -> i番目にxを足す
:param n:
"""
self.n = n
self.data = [0] * (n + 1)
self.each = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.data[i]
i -= i & -i
return s
def add(self, i, x):
self.each[i] += x
while i <= self.n:
self.data[i] += x
i += i & -i
def __repr__(self):
return str(self.each)
def inversion_num(A):
"""
リストAの転倒数を座標圧縮ありで求める。要BIT, compress
"""
N = len(A)
Z, _ = compress(A)
tree = BIT(N)
res = 0
for a in A:
za = Z[a]
res += tree.sum(za)
tree.add(za, 1)
return N * (N - 1) // 2 - res
def main():
N, M = NMI()
P = NLI()
x = inversion_num(P)
ans = (x+M-1)//M * M
if (ans-x) % 2:
print(-1)
else:
print(ans)
if __name__ == "__main__":
main()