結果

問題 No.366 ロボットソート
ユーザー くれちー
提出日時 2017-01-08 14:27:12
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 489 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 20,992 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-29 18:35:39
合計ジャッジ時間 1,387 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:13:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         scanf("%d %d", &n, &k);
      |         ^~~~~~~~~~~~~~~~~~~~~~
main.c:15:19: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |         rep(i, n) scanf("%d", &a[i]);
      |                   ^~~~~~~~~~~~~~~~~~

ソースコード

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