結果
| 問題 |
No.2382 Amidakuji M
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-14 22:13:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 38 ms / 2,000 ms |
| コード長 | 1,381 bytes |
| コンパイル時間 | 743 ms |
| コンパイル使用メモリ | 76,572 KB |
| 最終ジャッジ日時 | 2025-02-15 14:10:31 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#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";
}
}