結果
問題 | No.2382 Amidakuji M |
ユーザー |
👑 |
提出日時 | 2023-07-14 22:53:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 70 ms / 2,000 ms |
コード長 | 843 bytes |
コンパイル時間 | 3,846 ms |
コンパイル使用メモリ | 229,776 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-16 07:59:26 |
合計ジャッジ時間 | 5,281 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>using namespace std;#include <atcoder/all>using namespace atcoder;int main(void) {int n;cin >> n;long long m;cin >> m;vector<int> p(n);for(int i=0;i<n;++i){cin >> p[i];--p[i];}fenwick_tree<int> bit(n);long long cnt=0;for(int i=0;i<n;++i){bit.add(p[i],1);cnt+=bit.sum(p[i]+1,n);}if(m%2==0 && cnt%2){cout << -1 << endl;} else if(m%2==0){if(cnt%m==0){cout << cnt << endl;} else {cout << (cnt/m+1)*m << endl;}} else {long long ans=0;if(cnt%m==0){ans=cnt;} else {ans=(cnt/m+1)*m;}if(ans%2!=cnt%2){ans+=m;}cout << ans << endl;}return 0;}