結果
問題 |
No.2382 Amidakuji M
|
ユーザー |
|
提出日時 | 2023-07-14 21:35:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 28 ms / 2,000 ms |
コード長 | 618 bytes |
コンパイル時間 | 582 ms |
コンパイル使用メモリ | 71,684 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-16 06:27:29 |
合計ジャッジ時間 | 1,605 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include<iostream> #include<cassert> #include<atcoder/fenwicktree> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; long M; cin>>N>>M; atcoder::fenwick_tree<int>BIT(N); long K=0; for(int i=0;i<N;i++) { int P;cin>>P;P--; K+=BIT.sum(P,N); BIT.add(P,1); } if(K==0)cout<<"0\n"; else if(M%2==0) { if(K%2==1) { cout<<"-1\n"; } else { if(M<=K)cout<<(K+M-1)/M*M<<"\n"; else cout<<M<<"\n"; } } else { if(M<=K) { long t=(K+M-1)/M*M; if(K%2!=t%2)t+=M; cout<<t<<"\n"; } else if(K%2==M%2)cout<<M<<"\n"; else cout<<M+M<<"\n"; } }