結果

問題 No.2382 Amidakuji M
ユーザー eve__fuyukieve__fuyuki
提出日時 2023-10-10 17:26:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 803 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 201,584 KB
実行使用メモリ 4,824 KB
最終ジャッジ日時 2023-10-10 17:26:16
合計ジャッジ時間 3,960 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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) {
        cout << (m - 1 + inv) / m * m << 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