結果

問題 No.2382 Amidakuji M
ユーザー _____TAB__________TAB_____
提出日時 2023-07-15 13:27:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 74,804 KB
実行使用メモリ 5,544 KB
最終ジャッジ日時 2023-10-15 04:24:48
合計ジャッジ時間 2,453 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 27 ms
4,352 KB
testcase_04 AC 48 ms
5,016 KB
testcase_05 AC 8 ms
4,348 KB
testcase_06 AC 30 ms
4,348 KB
testcase_07 AC 19 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 42 ms
4,916 KB
testcase_10 AC 60 ms
5,112 KB
testcase_11 AC 22 ms
4,352 KB
testcase_12 AC 41 ms
4,756 KB
testcase_13 AC 1 ms
4,356 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 65 ms
5,488 KB
testcase_20 AC 65 ms
5,480 KB
testcase_21 AC 65 ms
5,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

#include <vector>
#include <cassert>

template<typename T>
struct BIT{//0-indexed
private:
  std::vector<T> data;
  const int n;
public:
  BIT(int sz) : data(sz+1,0), n(sz) {}

  T sum(size_t i) const noexcept {//sum[0,i)
    //++i;
    T s = 0;
    while(i > 0){
      s += data[i];
      i -= i&(-i);
    }
    return s;
  }

  T sum(size_t i, size_t j) const noexcept {// sum[i,j)
    return sum(j) - sum(i);
  }

  void add(int i, T x) noexcept {
    ++i;
    assert(i > 0);
    while(i <= n){
      data[i] += x;
      i += i&(-i);
    }
  }
};

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];
  }

  BIT<long long> bit(n);
  long long r = 0;
  for(auto p: P){
    r += bit.sum(p,n);
    bit.add(p,1);
  }

  long long ans = (r+m-1)/m*m;

  if((ans-r)%2) ans += m;

  if((ans-r)%2){
    cout << -1 << '\n';
    return 0;
  }

  cout << ans << '\n';
}
0