結果

問題 No.2382 Amidakuji M
ユーザー strangerxxxstrangerxxx
提出日時 2023-07-14 21:56:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,169 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 173,076 KB
最終ジャッジ日時 2023-10-14 12:05:23
合計ジャッジ時間 4,933 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 73 ms
71,408 KB
testcase_02 WA -
testcase_03 AC 177 ms
121,084 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 187 ms
126,584 KB
testcase_07 AC 150 ms
106,936 KB
testcase_08 AC 89 ms
76,740 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 234 ms
122,360 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 71 ms
71,284 KB
testcase_16 AC 73 ms
71,316 KB
testcase_17 AC 72 ms
71,616 KB
testcase_18 WA -
testcase_19 AC 316 ms
172,964 KB
testcase_20 AC 318 ms
172,720 KB
testcase_21 AC 317 ms
173,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def resolve():
    import sys

    input = sys.stdin.readline
    n, m = map(int, input().split())
    p = tuple(map(int, input().split()))
    i = inversion_num_compress(p)
    if (m - i) % 2 == 1:
        print(-1)
        return
    import math

    print(i + math.lcm(abs(m - i), 2))


def inversion_num_compress(l):
    s = set(l)
    d = {x: i for i, x in enumerate(sorted(s))}
    r = [d[x] + 1 for x in l]
    bit = BIT(len(s) + 1)
    res = 0
    for i, p in enumerate(r):
        bit.add(p, 1)
        res += i + 1 - bit.sum(p)
    return res


class BIT:
    # Binary Indexed Tree (Fenwick Tree)
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.el = [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):
        # assert i > 0
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)


if __name__ == "__main__":
    resolve()
0