結果

問題 No.2382 Amidakuji M
ユーザー miya145592miya145592
提出日時 2023-07-14 22:16:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 684 bytes
コンパイル時間 1,065 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 108,932 KB
最終ジャッジ日時 2023-10-14 12:30:23
合計ジャッジ時間 4,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,040 KB
testcase_01 AC 75 ms
71,136 KB
testcase_02 AC 77 ms
71,280 KB
testcase_03 AC 127 ms
89,160 KB
testcase_04 AC 164 ms
101,776 KB
testcase_05 AC 97 ms
78,496 KB
testcase_06 AC 128 ms
91,104 KB
testcase_07 AC 113 ms
84,680 KB
testcase_08 AC 88 ms
76,644 KB
testcase_09 AC 145 ms
95,892 KB
testcase_10 AC 186 ms
104,864 KB
testcase_11 AC 116 ms
86,416 KB
testcase_12 AC 151 ms
98,348 KB
testcase_13 AC 75 ms
71,188 KB
testcase_14 AC 76 ms
71,316 KB
testcase_15 AC 75 ms
71,316 KB
testcase_16 AC 74 ms
71,240 KB
testcase_17 AC 73 ms
71,132 KB
testcase_18 AC 74 ms
71,352 KB
testcase_19 AC 201 ms
108,720 KB
testcase_20 AC 198 ms
108,920 KB
testcase_21 AC 197 ms
108,932 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)
cnt = 0
for i, p in enumerate(P):
    cnt += i - bit.sum(p)
    bit.add(p, 1)
 
k = 0
while M*k<cnt:
    k+=1

if (M*k-cnt)%2==0:
    ans = M*k
    print(ans)
else:
    k+=1
    if (M*k-cnt)%2==0:
        ans = M*k
        print(ans)
    else:
        print(-1)

0