結果

問題 No.2382 Amidakuji M
ユーザー otoshigootoshigo
提出日時 2023-07-14 22:13:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 79,312 KB
実行使用メモリ 4,660 KB
最終ジャッジ日時 2023-10-14 12:26:12
合計ジャッジ時間 2,157 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 12 ms
4,352 KB
testcase_04 AC 19 ms
4,348 KB
testcase_05 AC 5 ms
4,352 KB
testcase_06 AC 13 ms
4,352 KB
testcase_07 AC 8 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 17 ms
4,348 KB
testcase_10 AC 23 ms
4,420 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 17 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 25 ms
4,576 KB
testcase_20 AC 25 ms
4,636 KB
testcase_21 AC 25 ms
4,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

template <class T>
struct FenwickTree{
    int n;
    vector<T> bit;
    FenwickTree(int N){
        bit.resize(N+1);
        n = N;
    }
    T sum(int i){
        i++;
        T s = 0;
        while (i > 0){
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }
    T sum(int l, int r){
        if (l >= r) return 0;
        return sum(r-1) - sum(l-1);
    }
    void add(int i, T x){
        i++;
        while (i <= n){
            bit[i] += x;
            i += i & -i;
        }
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    ll M;
    cin>>N>>M;
    vector<int>P(N);
    FenwickTree<int>fw(N);
    ll cnt=0;
    for(int i=0;i<N;i++){
        cin>>P[i];
        P[i]--;
        cnt+=fw.sum(P[i],N);
        fw.add(P[i],1);
    }
    if(cnt==0)cout<<0<<"\n";
    else if(cnt%2==0){
        if(M%2==0)cout<<max(cnt,M)<<"\n";
        else cout<<max(cnt,2*M)<<"\n";
    }else{
        if(M%2==0)cout<<-1<<"\n";
        else cout<<max(cnt,M)<<"\n";
    }
}
0