結果

問題 No.2382 Amidakuji M
ユーザー shobonvipshobonvip
提出日時 2023-07-14 21:31:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 1,000 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 109,296 KB
最終ジャッジ日時 2023-10-14 11:30:34
合計ジャッジ時間 4,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,452 KB
testcase_01 AC 68 ms
71,324 KB
testcase_02 AC 68 ms
71,308 KB
testcase_03 AC 140 ms
90,848 KB
testcase_04 AC 161 ms
101,236 KB
testcase_05 AC 94 ms
79,216 KB
testcase_06 AC 128 ms
92,668 KB
testcase_07 AC 111 ms
85,832 KB
testcase_08 AC 82 ms
76,492 KB
testcase_09 AC 151 ms
97,192 KB
testcase_10 AC 179 ms
105,256 KB
testcase_11 AC 116 ms
87,152 KB
testcase_12 AC 151 ms
98,736 KB
testcase_13 AC 67 ms
71,136 KB
testcase_14 AC 67 ms
71,200 KB
testcase_15 AC 69 ms
71,324 KB
testcase_16 AC 67 ms
71,440 KB
testcase_17 AC 67 ms
71,136 KB
testcase_18 AC 68 ms
71,396 KB
testcase_19 AC 184 ms
109,176 KB
testcase_20 AC 188 ms
109,296 KB
testcase_21 AC 188 ms
109,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
	def query(self, m):
		res = 0
		while m > 0:
			res += self.bit[m]
			m -= m&(-m)
		return res
	
	def sum(self, a, b):
		return self.query(b)-self.query(a)
	
	def sumall(self):
		bitlen = self.bitlen-1
		return self.query(bitlen)
	
	def add(self, m, x):
		m += 1
		bitlen = len(self.bit)
		while m <= bitlen-1:
			self.bit[m] += x
			m += m&(-m)
		return
	
	def search(self, a):
		tmpsum = 0
		i = 0
		k = (self.bitlen-1).bit_length()
		while k >= 0:
			tmpk = 2**k
			if i+tmpk <= self.bitlen-1:
				if tmpsum+self.bit[i+tmpk] < a:
					tmpsum += self.bit[i+tmpk]
					i += tmpk
			k = k-1
		return i+1
	
	def __init__(self, a):
		self.bitlen = a
		self.bit = [0]*a

n, m = map(int,input().split())
p = list(map(int,input().split()))

for i in range(n):
	p[i] -= 1

bit = BIT(n+4)
ans = 0

for i in range(n):
	ans += bit.sum(p[i]+1, n+2);
	bit.add(p[i], 1);

k = (ans + m -1) // m
k *= m

if k % 2 == ans % 2:
	print(k)
	exit()

k += m

if k % 2 == ans % 2:
	print(k)
	exit()

print(-1)
0