結果
| 問題 |
No.2382 Amidakuji M
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-14 22:13:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 589 bytes |
| コンパイル時間 | 1,568 ms |
| コンパイル使用メモリ | 194,860 KB |
| 最終ジャッジ日時 | 2025-02-15 14:10:38 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int BIT[200005];
int getsum(int l,int r){
int ret=0;
for(;r>0;r-=r&-r)ret+=BIT[r];
for(--l;l>0;l-=l&-l)ret-=BIT[l];
return ret;
}
void add(int p){
for(;p<200005;p+=p&-p)++BIT[p];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N,M;
cin>>N>>M;
vector<int>A(N);
for(int &i:A)cin>>i;
ll cnt=0;
for(int i=0;i<N;i++){
cnt+=getsum(A[i],N);
add(A[i]);
}
if(cnt%2==1&&M%2==0){
cout<<"-1\n";
}else{
ll t=(cnt+M-1)/M*M;
if(t%2!=cnt%2)t+=M;
cout<<t<<'\n';
}
}