結果

問題 No.2382 Amidakuji M
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-07-14 22:59:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,292 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 96,864 KB
最終ジャッジ日時 2023-10-14 13:21:50
合計ジャッジ時間 4,508 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,196 KB
testcase_01 AC 61 ms
71,340 KB
testcase_02 AC 62 ms
71,420 KB
testcase_03 AC 106 ms
84,680 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 106 ms
85,628 KB
testcase_07 AC 94 ms
81,532 KB
testcase_08 AC 74 ms
76,436 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 118 ms
90,204 KB
testcase_13 AC 61 ms
71,120 KB
testcase_14 AC 65 ms
71,344 KB
testcase_15 AC 63 ms
71,348 KB
testcase_16 AC 60 ms
71,240 KB
testcase_17 AC 61 ms
71,240 KB
testcase_18 WA -
testcase_19 AC 145 ms
96,624 KB
testcase_20 AC 152 ms
96,616 KB
testcase_21 AC 148 ms
96,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def build(self, list):
        self.tree[1:] = list.copy()
        for i in range(self.size+1):
            j = i + (i & (-i))
            if j < self.size+1:
                self.tree[j] += self.tree[i]

    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    # 総和がx以上になる位置のindex をbinary search
    def bsearch(self,x):
        le = 0
        ri = 1<<(self.size.bit_length()-1)
        while ri > 0:
            if le+ri <= self.size and self.tree[le+ri]<x:
                x -= self.tree[le+ri]
                le += ri
            ri >>= 1
        return le+1
        
        
n,m = map(int,input().split())
P = [int(x)-1 for x in input().split()]

num = 0
bit = BIT(n+1)

for i,p in enumerate(P):
	num += i-bit.sum(p)
	bit.add(p,1)


if num%m == 0:
	print(num)
elif num%2 != m%2 and num%2:
	print(-1)
else:
	dif = m - num%m
	print(num+dif)
0