結果

問題 No.2382 Amidakuji M
ユーザー Today03Today03
提出日時 2023-07-14 23:00:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 665 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 201,868 KB
実行使用メモリ 4,564 KB
最終ジャッジ日時 2023-10-14 13:24:21
合計ジャッジ時間 4,048 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 27 ms
4,348 KB
testcase_04 AC 46 ms
4,380 KB
testcase_05 AC 8 ms
4,352 KB
testcase_06 AC 29 ms
4,352 KB
testcase_07 AC 18 ms
4,352 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 40 ms
4,352 KB
testcase_10 AC 58 ms
4,352 KB
testcase_11 AC 21 ms
4,352 KB
testcase_12 AC 40 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 63 ms
4,564 KB
testcase_20 AC 62 ms
4,564 KB
testcase_21 AC 63 ms
4,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/fenwicktree>
long long inversion_number(vector<int> &a) {
  int n=a.size();
  atcoder::fenwick_tree<int> ft(n);
  long long ret=0;
  for (int i=0;i<n;i++) {
    ret+=i-ft.sum(0,a[i]);
    ft.add(a[i],1);
  }
  return ret;
}

int main() {
  int n;
  long long m;
  cin>>n>>m;
  vector<int> p(n);
  for (int i=0;i<n;i++) {
    cin>>p[i];
    p[i]--;
  }
  long long in=inversion_number(p);
  if (in==0) {
    cout<<0<<endl;
    return 0;
  }
  else if (in%2==1&&m%2==0) {
    cout<<-1<<endl;
    return 0;
  }
  long long tmp=(in+m-1)/m*m;
  while ((tmp-in)%2!=0) {
    tmp+=m;
  }
  cout<<tmp<<endl;
}
0