結果

問題 No.2382 Amidakuji M
ユーザー tassei903tassei903
提出日時 2023-07-14 21:38:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 141,060 KB
最終ジャッジ日時 2023-10-14 11:41:46
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
71,556 KB
testcase_01 AC 66 ms
71,284 KB
testcase_02 AC 66 ms
71,472 KB
testcase_03 AC 182 ms
118,612 KB
testcase_04 AC 258 ms
132,728 KB
testcase_05 AC 106 ms
85,264 KB
testcase_06 AC 191 ms
124,124 KB
testcase_07 AC 151 ms
105,796 KB
testcase_08 AC 86 ms
77,736 KB
testcase_09 AC 238 ms
133,444 KB
testcase_10 AC 307 ms
135,524 KB
testcase_11 AC 160 ms
109,344 KB
testcase_12 AC 239 ms
135,872 KB
testcase_13 AC 68 ms
71,232 KB
testcase_14 AC 66 ms
71,224 KB
testcase_15 AC 66 ms
71,428 KB
testcase_16 AC 66 ms
71,344 KB
testcase_17 AC 66 ms
71,280 KB
testcase_18 AC 66 ms
71,444 KB
testcase_19 AC 324 ms
141,060 KB
testcase_20 AC 324 ms
141,036 KB
testcase_21 AC 324 ms
140,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
# Binary Indexed Tree (Fenwick Tree)
# 0-indexed
class BIT:
    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 0 <= i < self.n
        i = i+1
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i
    def set(self, i, x):
        assert 0 <= i <= self.n
        self.add(i,x-self.get(i))
    def get(self, i, j=None):# j=Noneのときiを取得, [i, j)の和を取得
        if j is None:
            return self.el[i+1]
        return self.sum(j) - self.sum(i)
def tentou(ar):
    s = sorted(set(ar))
    d = {x:i for i, x in enumerate(s)}
    m = len(s)
    bit = BIT(m)
    ans = 0
    for i in range(n):
        ans += bit.get(d[ar[i]], m)
        bit.add(d[ar[i]], 1)
    return ans

n,m = na()
p = na()
z = tentou(p)

if z % 2 and m % 2 == 0:
    print(-1)
    exit()
else:
    x = (z + m -1)//m * m
    if (z-x)%2 == 0:
        print(x)
    else:
        print(x+m)
        
0