結果

問題 No.2382 Amidakuji M
ユーザー 👑 獅子座じゃない人獅子座じゃない人
提出日時 2023-07-14 22:53:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 843 bytes
コンパイル時間 4,262 ms
コンパイル使用メモリ 227,700 KB
実行使用メモリ 4,704 KB
最終ジャッジ日時 2023-10-14 13:14:15
合計ジャッジ時間 6,291 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 27 ms
4,356 KB
testcase_04 AC 47 ms
4,372 KB
testcase_05 AC 8 ms
4,352 KB
testcase_06 AC 30 ms
4,348 KB
testcase_07 AC 19 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 41 ms
4,352 KB
testcase_10 AC 59 ms
4,392 KB
testcase_11 AC 22 ms
4,352 KB
testcase_12 AC 41 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 64 ms
4,664 KB
testcase_20 AC 64 ms
4,704 KB
testcase_21 AC 64 ms
4,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

int main(void) {
    int n;
    cin >> n;
    long long m;
    cin >> m;
    vector<int> p(n);
    for(int i=0;i<n;++i){
        cin >> p[i];
        --p[i];
    }
    fenwick_tree<int> bit(n);
    long long cnt=0;
    for(int i=0;i<n;++i){
        bit.add(p[i],1);
        cnt+=bit.sum(p[i]+1,n);
    }
    if(m%2==0 && cnt%2){
        cout << -1 << endl;
    } else if(m%2==0){
        if(cnt%m==0){
            cout << cnt << endl;
        } else {
            cout << (cnt/m+1)*m << endl;
        }
    } else {
        long long ans=0;
        if(cnt%m==0){
            ans=cnt;
        } else {
            ans=(cnt/m+1)*m;
        }
        if(ans%2!=cnt%2){
            ans+=m;
        }
        cout << ans << endl;
    }
    return 0;
}
0