結果

問題 No.2382 Amidakuji M
ユーザー ikomaikoma
提出日時 2023-07-14 22:11:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 1,040 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 107,800 KB
最終ジャッジ日時 2023-10-14 12:24:23
合計ジャッジ時間 4,986 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,428 KB
testcase_01 AC 76 ms
71,412 KB
testcase_02 AC 77 ms
70,964 KB
testcase_03 AC 188 ms
88,796 KB
testcase_04 AC 167 ms
100,684 KB
testcase_05 AC 98 ms
78,680 KB
testcase_06 AC 131 ms
90,592 KB
testcase_07 AC 114 ms
84,256 KB
testcase_08 AC 88 ms
76,420 KB
testcase_09 AC 151 ms
95,236 KB
testcase_10 AC 191 ms
103,844 KB
testcase_11 AC 118 ms
85,748 KB
testcase_12 AC 149 ms
97,320 KB
testcase_13 AC 76 ms
71,124 KB
testcase_14 AC 76 ms
71,216 KB
testcase_15 AC 79 ms
71,196 KB
testcase_16 AC 80 ms
71,252 KB
testcase_17 AC 81 ms
71,076 KB
testcase_18 AC 76 ms
71,284 KB
testcase_19 AC 207 ms
107,540 KB
testcase_20 AC 202 ms
107,544 KB
testcase_21 AC 202 ms
107,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
class BIT:
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)
    def sum(self, i): # [0,i]
        i+=1
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s
    def sumrange(self, i, j): # [i,j)
        return self.sum(j-1) - self.sum(i-1)
    def add(self, i, x):
        if x==0:return
        i+=1
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

def inversion_number(A):
    bit = BIT(max(A)+1)
    ans = 0
    for i, a in enumerate(A):
        bit.add(a, 1)
        ans += i + 1 - bit.sum(a)
    return ans

N,M=map(int,input().split())
P=list(map(int,input().split()))
n = inversion_number(P)
# n + 偶数回 == M*a となる最小のMa
if n%2==1 and M%2==0:
    print(-1)
else:
    m = (n+M-1)//M*M
    if n%2 != m%2:
        m+=M
    print(m)
0