結果

問題 No.2382 Amidakuji M
ユーザー kotatsugamekotatsugame
提出日時 2023-07-14 21:35:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 618 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 70,080 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 11:37:54
合計ジャッジ時間 1,863 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 12 ms
4,348 KB
testcase_04 AC 19 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 12 ms
4,352 KB
testcase_07 AC 8 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 17 ms
4,348 KB
testcase_10 AC 25 ms
4,348 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 17 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 26 ms
4,348 KB
testcase_20 AC 26 ms
4,348 KB
testcase_21 AC 26 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cassert>
#include<atcoder/fenwicktree>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	long M;
	cin>>N>>M;
	atcoder::fenwick_tree<int>BIT(N);
	long K=0;
	for(int i=0;i<N;i++)
	{
		int P;cin>>P;P--;
		K+=BIT.sum(P,N);
		BIT.add(P,1);
	}
	if(K==0)cout<<"0\n";
	else if(M%2==0)
	{
		if(K%2==1)
		{
			cout<<"-1\n";
		}
		else
		{
			if(M<=K)cout<<(K+M-1)/M*M<<"\n";
			else cout<<M<<"\n";
		}
	}
	else
	{
		if(M<=K)
		{
			long t=(K+M-1)/M*M;
			if(K%2!=t%2)t+=M;
			cout<<t<<"\n";
		}
		else if(K%2==M%2)cout<<M<<"\n";
		else cout<<M+M<<"\n";
	}
}
0