結果

問題 No.2382 Amidakuji M
ユーザー ntudantuda
提出日時 2023-07-16 20:02:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 652 bytes
コンパイル時間 614 ms
コンパイル使用メモリ 81,776 KB
実行使用メモリ 107,584 KB
最終ジャッジ日時 2023-10-17 17:50:27
合計ジャッジ時間 4,539 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,512 KB
testcase_01 AC 36 ms
53,512 KB
testcase_02 AC 37 ms
53,512 KB
testcase_03 AC 91 ms
87,872 KB
testcase_04 AC 125 ms
99,832 KB
testcase_05 AC 61 ms
77,104 KB
testcase_06 AC 92 ms
89,604 KB
testcase_07 AC 77 ms
83,344 KB
testcase_08 AC 49 ms
68,560 KB
testcase_09 AC 112 ms
94,632 KB
testcase_10 AC 149 ms
103,872 KB
testcase_11 AC 82 ms
84,608 KB
testcase_12 AC 116 ms
96,872 KB
testcase_13 AC 37 ms
53,512 KB
testcase_14 AC 36 ms
53,512 KB
testcase_15 AC 36 ms
53,512 KB
testcase_16 AC 36 ms
53,512 KB
testcase_17 AC 36 ms
53,512 KB
testcase_18 AC 37 ms
53,512 KB
testcase_19 AC 157 ms
107,584 KB
testcase_20 AC 159 ms
107,584 KB
testcase_21 AC 160 ms
107,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, M = map(int, input().split())
P = list(map(int, input().split()))
bit = Bit(N + 1)
cnt = 0
for i, p in enumerate(P):
    bit.add(p, 1)
    cnt += (i + 1) - bit.sum(p)
tmp = - (- cnt // M)
if cnt % 2 == 1 and M % 2 == 0:
    print(-1)
elif (tmp * M - cnt) % 2 == 0:
    print(tmp * M)
else:
    print((tmp + 1) * M)
0