結果

問題 No.2382 Amidakuji M
ユーザー eve__fuyuki
提出日時 2023-10-10 17:28:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 892 bytes
コンパイル時間 1,859 ms
コンパイル使用メモリ 196,436 KB
最終ジャッジ日時 2025-02-17 06:39:34
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    long long m;
    cin >> n >> m;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    long long inv = 0;
    fenwick_tree<int> fw(n);
    for (int i = 0; i < n; i++) {
        inv += fw.sum(p[i] + 1, n);
        fw.add(p[i], 1);
    }
    if (inv % 2 == 0) {
        long long ans = (m - 1 + inv) / m * m;
        if (ans % 2 == 1) {
            ans += m;
        }
        cout << ans << endl;
    } else {
        if (m % 2 == 0) {
            cout << -1 << endl;
        } else {
            long long ans = (m - 1 + inv) / m * m;
            if (ans % 2 == 0) {
                ans += m;
            }
            cout << ans << endl;
        }
    }
}
0