結果

問題 No.2382 Amidakuji M
ユーザー Shell-WataruShell-Wataru
提出日時 2023-07-14 21:51:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,684 bytes
コンパイル時間 1,766 ms
コンパイル使用メモリ 133,628 KB
実行使用メモリ 7,876 KB
最終ジャッジ日時 2023-10-14 11:58:09
合計ジャッジ時間 3,269 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 26 ms
5,164 KB
testcase_04 AC 46 ms
6,504 KB
testcase_05 AC 8 ms
4,352 KB
testcase_06 AC 28 ms
5,124 KB
testcase_07 AC 18 ms
4,616 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 41 ms
6,016 KB
testcase_10 AC 54 ms
7,232 KB
testcase_11 AC 20 ms
4,656 KB
testcase_12 AC 37 ms
6,128 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 61 ms
7,768 KB
testcase_20 AC 64 ms
7,724 KB
testcase_21 AC 62 ms
7,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <cmath>
#include <iomanip>
#include <functional>
#include <random>
#include <set>
#include <atcoder/string>
#include <atcoder/math>
#include <unordered_map>
#include <climits>
using namespace std;
using ll = long long;
using namespace atcoder;

//zero indexed and vector
template< typename T>
class BIT
{
public:
    vector<T> data;
    BIT(long long n) : data(n) {}
    void add(int index, T v)
    {
        int N = data.size();
        for (int i = index; i < N; i |= i + 1)
        {
            data[i] += v;
        }
    }

    long long sum(int index)
    {
        T retValue = 0;
        for (int i = index; i >= 0; i = (i & (i + 1)) - 1)
        {
            retValue += data[i];
        }
        return retValue;
    }

    void show(){
        for (auto v:data){
            cout << v << endl;
        }
    }
};

// 0~N-1に注意,compressするか確認
ll inversion_number(vector<ll> &A){
    BIT<ll> bit(A.size()*2);
    ll ans = 0;
    for (int i = 0; i < A.size(); i++) {
        ans += i - bit.sum(A[i]);
        bit.add(A[i], 1);
    }
    return ans;
}

int solve()
{
  ll N,M;
  cin >> N >> M;
  vector<ll> P(N);
  for(int i = 0;i < N;i++){
    cin >> P[i];
    P[i]--;
  }
  ll k = inversion_number(P);
  ll ans1 = (k+M-1)/M*M;
  ll ans2 = (k+M-1)/M*M + M;
  if( (ans1 - k) % 2 == 0){
    cout << ans1 << endl;
  }else if((ans2- k) % 2 == 0){
    cout << ans2 << endl;
  }else{
    cout << -1 << endl;
  }
  return 0;
}

int main()
{
  // ll T;
  // cin >> T;
  // while(T--){
  solve();
  // }
  return 0;
}
0