結果

問題 No.2382 Amidakuji M
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-07-14 21:36:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 715 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 109,024 KB
最終ジャッジ日時 2023-10-14 11:38:47
合計ジャッジ時間 4,035 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,484 KB
testcase_01 AC 71 ms
71,516 KB
testcase_02 AC 71 ms
71,456 KB
testcase_03 AC 124 ms
89,608 KB
testcase_04 AC 155 ms
102,044 KB
testcase_05 AC 93 ms
78,592 KB
testcase_06 AC 125 ms
91,120 KB
testcase_07 AC 109 ms
85,044 KB
testcase_08 AC 84 ms
76,692 KB
testcase_09 AC 140 ms
96,084 KB
testcase_10 AC 176 ms
105,332 KB
testcase_11 AC 113 ms
86,324 KB
testcase_12 AC 146 ms
98,480 KB
testcase_13 AC 72 ms
71,312 KB
testcase_14 AC 70 ms
71,424 KB
testcase_15 AC 71 ms
71,392 KB
testcase_16 AC 72 ms
71,404 KB
testcase_17 AC 71 ms
71,132 KB
testcase_18 AC 72 ms
71,228 KB
testcase_19 AC 186 ms
109,024 KB
testcase_20 AC 188 ms
108,928 KB
testcase_21 AC 186 ms
108,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bitadd(a,w,bit): #aにwを加える(1-origin)
 
    x = a
    while x <= (len(bit)-1):
        bit[x] += w
        x += x & (-1 * x)
 
def bitsum(a,bit): #ind 1~aまでの和を求める
 
    ret = 0
    x = a
    while x > 0:
        ret += bit[x]
        x -= x & (-1 * x)
    return ret


N,M = map(int,input().split())

BIT = [0] * (N+1)
a = list(map(int,input().split()))

ans = 0
for i in range(N):

    ans += i - bitsum(a[i],BIT)
    bitadd(a[i],1,BIT)

if ans % M == 0:
    print (ans)
elif M % 2 == 0 and ans % 2 == 1:
    print (-1)
else:
    
    now = ans // M * M
    while (True):

        if now >= ans and (now-ans) % 2 == 0:
            print (now)
            break

        now += M
    
0