結果

問題 No.2382 Amidakuji M
ユーザー t98slidert98slider
提出日時 2023-07-15 15:38:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,146 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 169,084 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 02:01:26
合計ジャッジ時間 3,248 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template <class T> struct fenwick_tree {
    using U = T;

    public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll m, cnt = 0, cnt2;
    int n, v;
    cin >> n >> m;
    fenwick_tree<int> fw(n);
    for(int i = 0; i < n; i++){
        cin >> v;
        cnt += fw.sum(v--, n);
        fw.add(v, 1);
    }
    cnt2 = (cnt + m - 1) / m * m;
    if((cnt2 - cnt) % 2 == 0){
        cout << cnt2 << '\n';
        return 0;
    }
    cnt2 += m;
    cout << (cnt2 ^ cnt & 1 ? -1 : cnt2) << '\n';
}
0