結果

問題 No.366 ロボットソート
ユーザー snrnsidy
提出日時 2021-07-18 03:53:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 642 bytes
コンパイル時間 2,010 ms
コンパイル使用メモリ 199,920 KB
最終ジャッジ日時 2025-01-23 03:07:08
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n, k, t;
	vector <int> a, b;

	cin >> n >> k;

	for (int i = 0; i < n; i++)
	{
		cin >> t;
		a.push_back(t);
	}

	b = a;
	sort(b.begin(), b.end());

	int res = 0;

	for (int i = 0; i < n; i++)
	{
		int p = -1;
		for (int j = i; j < n; j++)
		{
			if (b[i] == a[j])
			{
				p = j;
				break;
			}
		}
		if ((p - i) % k != 0)
		{
			cout << -1 << '\n';
			return 0;
		}
		res += ((p - i) / k);
		for (int j = p; j > i; j -= k)
		{
			int tt = a[j];
			a[j] = a[j - k];
			a[j - k] = tt;
		}
	}
	cout << res << '\n';
	return 0;
}
0