結果

問題 No.2382 Amidakuji M
ユーザー hari64hari64
提出日時 2023-07-14 22:13:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,169 bytes
コンパイル時間 2,272 ms
コンパイル使用メモリ 206,764 KB
実行使用メモリ 9,388 KB
最終ジャッジ日時 2023-10-14 12:26:09
合計ジャッジ時間 3,883 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 30 ms
5,584 KB
testcase_04 AC 55 ms
7,756 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 33 ms
5,952 KB
testcase_07 AC 21 ms
4,848 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 48 ms
7,156 KB
testcase_10 AC 71 ms
8,812 KB
testcase_11 AC 24 ms
5,136 KB
testcase_12 AC 49 ms
7,176 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 77 ms
9,368 KB
testcase_20 AC 77 ms
9,332 KB
testcase_21 AC 77 ms
9,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef hari64
#include <bits/stdc++.h>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define debug(...)
#else
#include "util/viewer.hpp"
#define debug(...) viewer::_debug(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// clang-format off
using namespace std;
using ll = long long; using ld = long double;
using pii = pair<int, int>; using pll = pair<ll, ll>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>; using vl = vc<ll>; using vpi = vc<pii>; using vpl = vc<pll>;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
constexpr int INF = 1001001001; constexpr long long INFll = 1001001001001001001;
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
// clang-format on

template <class T>
struct fenwick_tree {
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}
    explicit fenwick_tree(vector<T>& As) : _n(As.size()), data(As) {}
    // A[idx]+=x
    void add(int idx, T x) {
        assert(0 <= idx && idx < _n);
        idx++;
        while (idx <= _n) {
            data[idx - 1] += x;
            idx += idx & -idx;
        }
    }
    // Σ_[l,r)
    T sum(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        return _sum(r) - _sum(l);
    }
    // Σ_[0,r)
    T sum(int r) const {
        assert(0 <= r && r <= _n);
        return _sum(r);
    }
    // A[idx] O(logN)
    T get(int idx) const {
        assert(0 <= idx && idx < _n);
        return sum(idx, idx + 1);
    }
    // debug
    vector<T> state() const {
        vector<T> ret(_n);
        for (int i = 0; i < _n; i++) ret[i] = get(i);
        return ret;
    }

    inline T operator[](int idx) const { return get(idx); }

   private:
    int _n;
    vector<T> data;
    T _sum(int r) const {
        T s = 0;
        while (r > 0) s += data[r - 1], r -= r & -r;
        return s;
    }
};

template <class T>  // https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_5_D
long long inversion_number(const vector<T>& As) {
    vector<T> Bs = As, Xs(As.size());
    sort(Bs.begin(), Bs.end());
    Bs.erase(unique(Bs.begin(), Bs.end()), Bs.end());
    assert(Bs.size() == As.size());
    for (int i = 0; i < (int)As.size(); i++)
        Xs[i] = lower_bound(Bs.begin(), Bs.end(), As[i]) - Bs.begin();
    int N = Xs.size();
    long long ret = 0;
    fenwick_tree<T> f(N);
    for (int j = 0; j < N; j++) {
        ret += j - f.sum(Xs[j]);
        f.add(Xs[j], 1);
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, M;
    cin >> N >> M;

    vl Ps(N);
    for (auto& P : Ps) cin >> P;

    ll inv = inversion_number(Ps);

    if (inv % M == 0) {
        cout << inv << endl;
    } else if ((M - inv % M) % 2 == 0) {
        cout << inv + (M - inv % M) << endl;
    } else if (M % 2 == 0) {
        cout << -1 << endl;
    } else {
        cout << inv + (M - inv % M) + M << endl;
    }

    return 0;
}
0