結果

問題 No.2382 Amidakuji M
ユーザー sharusharu
提出日時 2023-07-15 00:08:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 977 ms / 2,000 ms
コード長 838 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 29,964 KB
最終ジャッジ日時 2023-10-14 14:33:07
合計ジャッジ時間 9,667 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,796 KB
testcase_01 AC 16 ms
7,872 KB
testcase_02 AC 16 ms
7,768 KB
testcase_03 AC 404 ms
17,416 KB
testcase_04 AC 728 ms
25,420 KB
testcase_05 AC 116 ms
10,640 KB
testcase_06 AC 466 ms
19,060 KB
testcase_07 AC 269 ms
14,704 KB
testcase_08 AC 32 ms
8,656 KB
testcase_09 AC 696 ms
23,148 KB
testcase_10 AC 977 ms
29,364 KB
testcase_11 AC 298 ms
15,604 KB
testcase_12 AC 696 ms
23,136 KB
testcase_13 AC 16 ms
7,872 KB
testcase_14 AC 16 ms
7,932 KB
testcase_15 AC 16 ms
7,824 KB
testcase_16 AC 16 ms
7,780 KB
testcase_17 AC 16 ms
7,872 KB
testcase_18 AC 16 ms
7,796 KB
testcase_19 AC 962 ms
29,912 KB
testcase_20 AC 974 ms
29,916 KB
testcase_21 AC 961 ms
29,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class fenwick_tree:
    def __init__(self, N):
        self.n = N
        self.data = [0] * N

    def add(self, p, x):
        assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """return sum(a[l:r-1])"""
        assert 0 <= l and l <= r and r <= self.n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s


n, m = map(int, input().split())
p = list(map(int, input().split()))
ft = fenwick_tree(n)
l = 0
for i in range(n):
    l += ft.sum(p[i], n)
    ft.add(p[i] - 1, 1)

if l % 2 == 1 and m % 2 == 0:
    print(-1)
else:
    ans = 0 - -l // m * m
    if ans % 2 != l % 2:
        ans += m
    print(ans)
0