結果

問題 No.2382 Amidakuji M
ユーザー matsup10matsup10
提出日時 2023-07-15 00:34:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 5,697 ms
コンパイル使用メモリ 315,696 KB
実行使用メモリ 4,984 KB
最終ジャッジ日時 2023-10-14 14:54:31
合計ジャッジ時間 8,423 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 26 ms
4,352 KB
testcase_04 AC 47 ms
4,348 KB
testcase_05 AC 8 ms
4,352 KB
testcase_06 AC 29 ms
4,348 KB
testcase_07 AC 19 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 41 ms
4,348 KB
testcase_10 AC 59 ms
4,436 KB
testcase_11 AC 21 ms
4,352 KB
testcase_12 AC 41 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 64 ms
4,756 KB
testcase_20 AC 63 ms
4,984 KB
testcase_21 AC 63 ms
4,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>
#include <atcoder/all>

using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0;i<int(n);i++)
#define FOR(i,a,b) for(int i=a;i<=int(b);i++)
#define ALL(x) x.begin(),x.end()
#define INF INT_MAX
#define INFL LLONG_MAX / 4
using namespace atcoder;
using mint = modint998244353;
template<typename T> void chmin(T& a, T b) { a = min(a, b); }
template<typename T> void chmax(T& a, T b) { a = max(a, b); }
#define PR(x) cerr << #x << "=" << x << endl
using i128 = __int128_t;
template <typename T>
struct BIT {
  int n;          // 配列の要素数(数列の要素数+1)
  vector<T> bit;  // データの格納先(1-index)
  BIT(int n_) : n(n_ + 1), bit(n, 0) {}
  void add(int idx, T x) {
    for (int i = idx; i < n; i += (i & -i)) {
      bit[i] += x;
    }
  }
  T sum(int idx) {
    T s(0);
    for (int i = idx; i > 0; i -= (i & -i)) {
      s += bit[i];
    }
    return s;
  }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<int> p(n);
  REP(i, n) cin >> p[i];

  BIT<int> bit(n);

  ll cnt = 0;
  REP(i, n) {
    bit.add(p[i], 1);
    cnt += bit.sum(n) - bit.sum(p[i]);
  }

  if(cnt == 0) {
    cout << 0;
    return 0;
  }

  ll nm = (cnt + m - 1) / m * m;

  if(nm % 2 == cnt % 2) {
    cout << nm;
    return 0;
  }
  nm += m;

  if(nm % 2 == cnt % 2) {
    cout << nm;
  } else {
    cout << -1;
  }

  return 0;
}
0