結果

問題 No.2382 Amidakuji M
ユーザー achapiachapi
提出日時 2023-07-14 21:43:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 694 bytes
コンパイル時間 3,994 ms
コンパイル使用メモリ 259,968 KB
実行使用メモリ 4,724 KB
最終ジャッジ日時 2023-10-14 11:47:46
合計ジャッジ時間 5,717 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

int main() {
	long long N, M;
	cin >> N >> M;
	vector<int> P(N);
	for (int i = 0; i < N; i++){
		cin >> P[i];
		P[i]--;
	}
	atcoder::fenwick_tree<int> fw(N);
	long long c = 0;
	for (int i = 0; i < N; i++){
		c += fw.sum(P[i], N);
		fw.add(P[i], 1);
	}
	long long ans = 0;
	if (c % M == 0){
		ans = c;
	} else if (c < M){
		if ((M - c) % 2 == 0){
			ans = M;
		} else {
			if (M % 2 == 1){
				ans = M * 2;
			} else {
				ans = -1;
			}
		}
	} else {
		M = (c + M - 1) / M * M;
		if ((M - c) % 2 == 0){
			ans = M;
		} else {
			if (M % 2 == 1){
				ans = M * 2;
			} else {
				ans = -1;
			}
		}
	}
	cout << ans << '\n';
}
0