結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 11 ms
4,368 KB
testcase_04 AC 19 ms
4,372 KB
testcase_05 AC 4 ms
4,368 KB
testcase_06 AC 12 ms
4,368 KB
testcase_07 AC 9 ms
4,368 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 17 ms
4,372 KB
testcase_10 AC 25 ms
4,476 KB
testcase_11 AC 9 ms
4,372 KB
testcase_12 AC 17 ms
4,368 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 1 ms
4,368 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 25 ms
4,664 KB
testcase_20 AC 25 ms
4,668 KB
testcase_21 AC 26 ms
4,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
  }
}
0