結果

問題 No.2382 Amidakuji M
ユーザー monnumonnu
提出日時 2023-07-14 22:03:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,592 bytes
コンパイル時間 1,519 ms
コンパイル使用メモリ 169,224 KB
実行使用メモリ 4,768 KB
最終ジャッジ日時 2023-10-14 12:14:28
合計ジャッジ時間 3,051 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 27 ms
4,348 KB
testcase_04 AC 45 ms
4,352 KB
testcase_05 AC 8 ms
4,352 KB
testcase_06 AC 28 ms
4,348 KB
testcase_07 AC 17 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 39 ms
4,348 KB
testcase_10 AC 57 ms
4,352 KB
testcase_11 AC 19 ms
4,348 KB
testcase_12 AC 38 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 60 ms
4,768 KB
testcase_20 AC 58 ms
4,680 KB
testcase_21 AC 59 ms
4,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD (998244353)

class BIT
{
    int n;
    vector<int> a;

public:
    BIT(int n_) : n(n_)
    {
        a.resize(n + 1, 0);
    }
    void add(int i, int x)
    {
        while (i <= n)
        {
            a[i] += x;
            i += i & (-i);
        }
    }
    int sum(int i)
    {
        int ret = 0;
        while (i > 0)
        {
            ret += a[i];
            i -= i & (-i);
        }
        return ret;
    }
    int sum(int l, int r)
    {
        return sum(r) - sum(l - 1);
    }
};

ll modpow(ll a, ll x)
{
    ll ret = 1;
    a %= MOD;
    while (x > 0)
    {
        if (x % 2 == 1)
        {
            ret = ret * a % MOD;
        }
        a = a * a % MOD;
        x >>= 1;
    }
    return ret;
}

int main()
{
    int N;
    ll M;
    cin >> N >> M;
    vector<int> P(N);
    BIT tree(N);
    ll sum = 0;
    for (int i = 0; i < N; i++)
    {
        cin >> P[i];
        sum += (ll)tree.sum(P[i] + 1, N);
        tree.add(P[i], 1);
    }

    if (sum == 0)
    {
        cout << 0 << endl;
        return 0;
    }

    if (sum % 2 == 0)
    {
        ll ans = ((sum - 1) / M + 1) * M;
        if (ans % 2 == 1)
        {
            ans += M;
        }
        cout << ans << endl;
    }
    else
    {
        if (M % 2 == 0)
        {
            cout << -1 << endl;
        }
        else
        {
            ll ans = ((sum - 1) / M + 1) * M;
            if (ans % 2 == 0)
            {
                ans += M;
            }
            cout << ans << endl;
        }
    }
}
0