結果

問題 No.366 ロボットソート
ユーザー くれちーくれちー
提出日時 2017-01-08 14:27:12
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 489 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 24,220 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-28 15:31:04
合計ジャッジ時間 1,222 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,504 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 0 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 148 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdbool.h>
#define rep(i, n) for (i = 0; i < n; i++)

void swap(int *a, int *b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	int i, a[1000];
	rep(i, n) scanf("%d", &a[i]);

	int cnt = 0;
	rep(i, n) {
		if (i + k >= n) break;
		if (a[i] > a[i + k]) {
			swap(&a[i], &a[i + k]);
			cnt++; i = -1;
		}
	}
	rep(i, n - 1) {
		if (a[i] > a[i + 1]) {
			puts("-1");
			return 0;
		}
	}

	printf("%d\n", cnt);
	return 0;
}
0