結果

問題 No.2382 Amidakuji M
ユーザー SSRS
提出日時 2023-07-14 21:30:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 634 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 169,940 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-16 06:20:08
合計ジャッジ時間 3,049 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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]--;
  }
  vector<int> bit(N + 1, 0);
  long long C = 0;
  for (int i = 0; i < N; i++){
    C += i;
    int p = P[i];
    while (p > 0){
      C -= bit[p];
      p -= p & -p;
    }
    p = P[i] + 1;
    while (p <= N){
      bit[p]++;
      p += p & -p;
    }
  }
  long long A = (C + M - 1) / M * M;
  if ((A - C) % 2 == 0){
    cout << A << endl;
  } else {
    if (M % 2 == 0){
      cout << -1 << endl;
    } else {
      cout << A + M << endl;
    }
  }
}
0