結果

問題 No.2382 Amidakuji M
ユーザー a01sa01toa01sa01to
提出日時 2023-07-14 21:39:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,482 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 204,604 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2023-10-14 11:42:19
合計ジャッジ時間 4,081 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 56 ms
5,084 KB
testcase_04 AC 98 ms
6,872 KB
testcase_05 AC 16 ms
4,348 KB
testcase_06 AC 62 ms
5,348 KB
testcase_07 AC 38 ms
4,368 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 84 ms
6,288 KB
testcase_10 AC 124 ms
7,728 KB
testcase_11 AC 44 ms
4,656 KB
testcase_12 AC 84 ms
6,004 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,356 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 136 ms
8,064 KB
testcase_20 AC 136 ms
8,000 KB
testcase_21 AC 135 ms
8,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
  #define _GLIBCXX_DEBUG
#else
  #define Debug(...) void(0)
#endif
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)

// Usage: T = 転倒数の型、U = 要素の型
template<typename T, typename U>
pair<T, vector<U>> invNum(vector<U> a) {
  size_t n = a.size();
  if (n <= 1) return { (T) 0, a };
  size_t mid = n / 2;
  auto left = invNum<T, U>(vector<U>(a.begin(), a.begin() + mid));
  auto right = invNum<T, U>(vector<U>(a.begin() + mid, a.end()));
  vector<U> res;
  T inv = 0;
  inv += left.first + right.first;
  size_t i = 0, j = 0;
  while (i < left.second.size() && j < right.second.size()) {
    if (left.second[i] <= right.second[j]) {
      res.push_back(left.second[i]);
      i++;
    }
    else {
      res.push_back(right.second[j]);
      j++;
      inv += left.second.size() - i;
    }
  }
  while (i < left.second.size()) {
    res.push_back(left.second[i]);
    i++;
  }
  while (j < right.second.size()) {
    res.push_back(right.second[j]);
    j++;
  }
  return { inv, res };
}

int main() {
  int n;
  ll m;
  cin >> n >> m;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  auto [inv, q] = invNum<ll, int>(p);
  Debug(inv);
  ll minim = (inv + m - 1) / m * m;
  if (minim % 2 == inv % 2) {
    cout << minim << endl;
  }
  else if ((minim + m) % 2 == inv % 2) {
    cout << minim + m << endl;
  }
  else {
    cout << -1 << endl;
  }
  return 0;
}
0