結果

問題 No.2382 Amidakuji M
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-20 17:18:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,215 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 204,732 KB
実行使用メモリ 6,444 KB
最終ジャッジ日時 2023-10-20 17:49:09
合計ジャッジ時間 3,527 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<class T> struct BIT {
    vector<T> bit;
    int N;

    BIT (int n){
        N = n;
        bit.resize(N);
    }

    void add(int loc, T val){
        loc++;
        while(loc <= N){
            bit[loc-1] += val;
            loc += loc & -loc;
        }
    }

    T sum(int l, int r){
        T res = _sum(r) - _sum(l-1);
        return res;
    }

    T _sum(int r){
        r++;
        T res = 0;
        while(r > 0){
            res += bit[r-1];
            r -= r & -r;
        }
        return res;
    }
};

ll inversion_number(vector<ll> &A){
    ll d=0, mx=0;
    for (int i=0; i<A.size(); i++) mx = max(mx, A[i]);
    BIT<ll> tree(mx+1);

    for (int i=0; i<A.size(); i++){
        d += tree.sum(A[i]+1, mx);
        tree.add(A[i], 1);
    }

    return d;
}

int main(){

    ll N, M, I, K;
    cin >> N >> M;
    vector<ll> A(N);
    for (int i=0; i<N; i++) cin >> A[i];

    I = inversion_number(A);
    cout << I << endl;
    K = (I+M-1)/M;
    for (ll i=K; i<=K+1; i++){
        if ((M*i-I) % 2 == 0){
            cout << M*i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}
0