結果

問題 No.366 ロボットソート
コンテスト
ユーザー Vũ Tuấn Anh
提出日時 2026-02-10 17:04:34
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
TLE  
実行時間 -
コード長 841 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,235 ms
コンパイル使用メモリ 217,856 KB
実行使用メモリ 11,172 KB
最終ジャッジ日時 2026-02-10 17:04:40
合計ジャッジ時間 6,589 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 TLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

const int INF = 2e9;
const int MN = 105;
const int MOD = 1000000007;
const int LOG = 20;

int n,k;
int a[MN];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	int ans = 0;
	cin >> n >> k;
	for (int i=1; i<=n; i++){
		cin >> a[i];
	}
	vector<int> g[k];
	for (int i=1; i<=n; i++){
		g[i%k].push_back(a[i]);
	}
	for (int i=0; i<k; i++) {
		int m = g[i].size();
		for (int j=0; j<m; j++) {
			for (int x=j+1; x<m; x++) {
				if (g[i][j] > g[i][x]) {
					ans++;
				}
			}
		}
		sort(g[i].begin(), g[i].end());
	}
	for (int i=0; i<k; i++){
		for (int j=0; j<g[i].size(); j++){
			if (i!=0) a[j*k+i] = g[i][j];
			else a[(j+1)*k] = g[i][j];
		}
	}
	for (int i=2; i<=n; i++){
		if (a[i]<a[i-1]) {
			cout << -1;
			return 0;
		}
	}
	cout << ans;
	
    return 0;
}
		




0