結果

問題 No.2382 Amidakuji M
ユーザー coindarwcoindarw
提出日時 2023-07-14 22:09:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,261 bytes
コンパイル時間 5,093 ms
コンパイル使用メモリ 265,900 KB
実行使用メモリ 6,252 KB
最終ジャッジ日時 2023-10-14 12:23:06
合計ジャッジ時間 6,575 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 20 ms
4,352 KB
testcase_04 AC 35 ms
5,424 KB
testcase_05 AC 6 ms
4,352 KB
testcase_06 AC 22 ms
4,576 KB
testcase_07 AC 14 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 30 ms
5,160 KB
testcase_10 AC 44 ms
6,072 KB
testcase_11 AC 16 ms
4,352 KB
testcase_12 AC 31 ms
5,104 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 48 ms
6,252 KB
testcase_20 AC 48 ms
6,168 KB
testcase_21 AC 48 ms
6,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/all>
using ll = long long;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); ++i)
constexpr int inf = 2000'000'000;
constexpr ll linf = 4000000000000000000ll;
constexpr ll M7 = 1000000007ll;
constexpr ll M09 = 1000000009ll;
constexpr ll M9 = 998244353ll;
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using namespace atcoder;
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& v) {
    for (auto& e : v) os << e << " ";
    return os;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) noexcept {
    return os << "(" << p.first << ", " << p.second << ")";
}
#ifdef ONLINE_JUDGE
#define debug(...)
#else
#define debug(...) cerr << "<" << #__VA_ARGS__ << ">: ", debug_out(__VA_ARGS__)
template <typename T>
void debug_out(T t) {
    cerr << t << endl;
}
template <typename T, typename... Args>
void debug_out(T t, Args... args) {
    cerr << t << ", ";
    debug_out(args...);
}
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m;
    cin >> n >> m;
    vector<int> p(n);
    rep(i, n) cin >> p[i];
    auto inv = [](const vector<int>& v) {
        fenwick_tree<int> bit(v.size());
        using P = pair<int, int>;
        vector<P> v_idx(v.size());
        rep(i, v.size()) {
            v_idx.at(i) = {v.at(i), i};
        }
        sort(rall(v_idx));
        ll ans = 0;
        rep(i, v.size()) {
            ans += bit.sum(0, v_idx.at(i).second);
            bit.add(v_idx.at(i).second, 1);
        }
        return ans;
    };
    ll invNum = inv(p);
    debug(invNum);
    if (m % 2 == 0 && invNum % 2 != 0) {
        cout << -1 << endl;
        return 0;
    }
    ll c = (invNum + m - 1) / m * m;
    debug(c);
    if (c % 2 == invNum % 2) {
        cout << c << "\n";
    } else {
        cout << c + m << "\n";
    }
    return 0;
}
0