結果

問題 No.2382 Amidakuji M
ユーザー ococonomy1ococonomy1
提出日時 2023-07-14 22:06:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 3,352 bytes
コンパイル時間 1,188 ms
コンパイル使用メモリ 112,836 KB
実行使用メモリ 5,944 KB
最終ジャッジ日時 2023-10-14 12:18:42
合計ジャッジ時間 3,023 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 33 ms
4,444 KB
testcase_04 AC 63 ms
5,608 KB
testcase_05 AC 9 ms
4,352 KB
testcase_06 AC 36 ms
4,444 KB
testcase_07 AC 22 ms
4,352 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 50 ms
4,616 KB
testcase_10 AC 77 ms
5,628 KB
testcase_11 AC 26 ms
4,348 KB
testcase_12 AC 53 ms
4,556 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 84 ms
5,884 KB
testcase_20 AC 84 ms
5,888 KB
testcase_21 AC 85 ms
5,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
using lg = long long;
using pii = pair<int, int>;
using pll = pair<lg, lg>;
#define TEST cerr << "TEST" << endl
#define IINF 2147483647
#define LLINF 9223372036854775807LL
#define AMARI 998244353
// #define AMARI 1000000007
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'


//抽象再帰セグ木?(抽象セグ木の定義分からんな)(遅延ではない)
//ococo_segtree<int> rsq(n,0);みたいに宣言する
template<typename T>
class ococo_segtree {
    T func(T a, T b) {
        //ここに演算を入れる
        return (a + b);
    }

public:
    int n;
    T tmax;
    //range minimum query
    vector<T> rmq;

    //(データの大きさ,単位元)
    ococo_segtree(int N = 0, T t = IINF) {
        syokica(N, t);
    }

    //配列の初期化 O(N)
    void syokica(int a, T t) {
        n = 1;
        tmax = t;
        while (n < a)n *= 2;
        rmq.resize(2 * n - 1);
        for (int i = 0; i < 2 * n - 1; i++) {
            rmq[i] = tmax;
        }
    }

    //a[i]をxにする O(logN)
    void update_num(int i, T x) {
        i += (n - 1);
        rmq[i] = x;
        while (i) {
            i = (i - 1) / 2;
            rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
        }
    }

    //a[i]にxを加える O(logN)
    void add_num(int i, T x) {
        i += (n - 1);
        rmq[i] = func(rmq[i], x);
        while (i) {
            i = (i - 1) / 2;
            rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
        }
    }

    T get_minimum2(int a, int b, int k, int l, int r) {
        if (a <= l && r <= b)return rmq[k];
        else if (r <= a || b <= l)return tmax;
        else return func(get_minimum2(a, b, k * 2 + 1, l, (l + r) / 2), get_minimum2(a, b, k * 2 + 2, (l + r) / 2, r));
    }
    //[l,r)の区間演算の取得 O(logN)
    T get_val(int l, int r) {
        return get_minimum2(l, r, 0, 0, n);
    }
};


#define MULTI_TEST_CASE false
void solve(void) {
    int n;
    lg m;
    cin >> n >> m;
    vector<int> p(n);
    for(int i = 0; i < n; i++)cin >> p[i];
    lg tentousuu = 0;
    ococo_segtree<int> os(n + 2,0);
    for(int i = 0; i < n; i++){
        tentousuu += os.get_val(p[i],n + 2);
        os.add_num(p[i],1);
    }
    if(m % 2 == 0 && tentousuu % 2 == 1){
        cout << -1 << el;
        return;
    }
    if(tentousuu % m == 0){
        cout << tentousuu << el;
        return;
    }
    lg ans = tentousuu;
    ans += (m - (ans % m));
    if(ans % 2 == tentousuu % 2){
        cout << ans << el;
        return;
    }
    ans += m;
    if(ans % 2 == tentousuu % 2){
        cout << ans << el;
        return;
    }
    cout << -1 << el;
    return;
}

void calc(void) {
    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if (MULTI_TEST_CASE) cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
0