結果

問題 No.2382 Amidakuji M
ユーザー nikajnikaj
提出日時 2023-07-14 21:32:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 2,418 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 146,640 KB
実行使用メモリ 5,024 KB
最終ジャッジ日時 2023-10-14 11:33:54
合計ジャッジ時間 2,656 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 13 ms
4,352 KB
testcase_04 AC 23 ms
4,708 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 15 ms
4,348 KB
testcase_07 AC 10 ms
4,352 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 19 ms
4,356 KB
testcase_10 AC 28 ms
4,784 KB
testcase_11 AC 10 ms
4,352 KB
testcase_12 AC 20 ms
4,436 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 30 ms
4,940 KB
testcase_20 AC 30 ms
5,024 KB
testcase_21 AC 31 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F0(i,n) for (int i=0; i<n; i++)
#define F1(i,n) for (int i=1; i<=n; i++)
#define CL(a,x) memset(x, a, sizeof(x));
#define SZ(x) ((int)x.size())
const int inf = 1000009;
const double pi = acos(-1.0);
typedef pair<int, int> pii;
typedef long long ll;
const double EPS = 1e-9;
const int MOD = 998244353;
#define PR(x) cerr << #x << "=" << x << endl
template<class A, class B>
ostream& operator<<(ostream& os, const pair<A, B>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template<class A, class B, class C>
ostream& operator<<(ostream& os, const tuple<A, B, C>& p) { os << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")"; return os; }
istream& operator>>(istream& is, pii& p) { is>>p.first>>p.second; return is; }
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; F0(i,SZ(v)) { if (i>0) os << ","; os << v[i]; } os << "]"; return os; }
template<class T>
ostream& operator<<(ostream& os, const set<T>& v) { os << "{"; int f=1; for(auto i:v) { if(f)f=0;else os << ","; cerr << i; } os << "}" << endl; return os; }
template<class T, class R>
ostream& operator<<(ostream& os, const map<T,R>& v) { os << "{"; int f=1; for(auto i:v) { if(f)f=0;else os << ", "; cerr << i.first << ":" << i.second; } os << "}" << endl; return os; }

int i, j, k, n;
ll m, ans;
const int N = 200005;
const int M = 435;
int a[N], h[N];

const int DX[]={-1,0,1,0};
const int DY[]={0,1,0,-1};
const string CS="nesw";
const string HS="URDL";

void Add(int& x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
}


ll Mult(int x, int y) {
    return 1LL * x * y % MOD;
}

int get(int i) {
    int ret = 0;
    while (i) { ret += h[i]; i &= (i-1); }
    return ret;
}
void add(int i) {
    while (i <= n) { h[i]++; i = (i|(i-1))+1; }
}

void Solve() {
    F1(i, n) { scanf("%d", &a[i]); h[i] = 0; }
    //PR(m);
    ll inv = 0;
    for (int i = n; i >= 1; i--) {
        //PR(a[i]);
        inv += get(a[i] - 1);
        add(a[i]);
    }
    ans = ((inv + m  - 1) / m) * m;
    if (ans % 2 != inv % 2) {
        ans += m;
        if (ans % 2 != inv % 2) ans = -1;
    }
    cout << ans << endl;
}

int main() {
    //ignore = freopen("x.in", "r", stdin);
    //freopen("x.out", "w", stdout);

    //int tn; cin >> tn;
    //F1(ti, tn) {
    while (cin >> n >> m) {
        //PR(n);

        Solve();
    }


    return 0;
}
0