結果

問題 No.2382 Amidakuji M
ユーザー minimumminimum
提出日時 2023-07-14 22:09:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 885 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 86,804 KB
実行使用メモリ 108,636 KB
最終ジャッジ日時 2023-10-14 12:22:43
合計ジャッジ時間 3,927 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
70,968 KB
testcase_01 AC 69 ms
70,888 KB
testcase_02 AC 72 ms
70,868 KB
testcase_03 AC 125 ms
90,492 KB
testcase_04 AC 162 ms
100,844 KB
testcase_05 AC 94 ms
78,480 KB
testcase_06 AC 133 ms
92,036 KB
testcase_07 AC 110 ms
85,384 KB
testcase_08 AC 86 ms
77,092 KB
testcase_09 AC 151 ms
96,812 KB
testcase_10 AC 175 ms
104,580 KB
testcase_11 AC 115 ms
87,192 KB
testcase_12 AC 152 ms
98,248 KB
testcase_13 AC 67 ms
71,140 KB
testcase_14 AC 66 ms
71,120 KB
testcase_15 AC 66 ms
70,960 KB
testcase_16 AC 67 ms
70,872 KB
testcase_17 AC 66 ms
70,940 KB
testcase_18 AC 68 ms
70,820 KB
testcase_19 AC 188 ms
108,508 KB
testcase_20 AC 189 ms
108,604 KB
testcase_21 AC 187 ms
108,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class FenwickTree:

    def __init__(self, N):
        self.N = N
        self.d = [0] * N
    
    def add(self, p, x):
        assert 0 <= p < self.N
        p += 1
        while p <= self.N:
            self.d[p - 1] += x
            p += p & (-p)
    
    def sum0(self, r):
        s = 0
        while r > 0:
            s += self.d[r - 1]
            r -= r & (-r)
        return s
    
    def get_sum(self, l, r):
        assert 0 <= l <= r <= self.N
        return self.sum0(r) - self.sum0(l)


N, M = map(int, input().split())
P = list(map(lambda x: x - 1, map(int, input().split())))
fw = FenwickTree(N)

invs = 0
for i in range(N):
    invs += fw.get_sum(P[i], N)
    fw.add(P[i], 1)
if invs == 0:
    print(0)
elif M % 2 == 0 and invs % 2 == 1:
    print(-1)
else:
    t = M * ((invs + M - 1) // M)
    if t % 2 == invs % 2:
        print(t)
    else:
        print(t + M)
0