結果

問題 No.366 ロボットソート
ユーザー FF256grhy
提出日時 2016-04-30 01:43:39
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 530 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 23,552 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-29 17:47:44
合計ジャッジ時間 1,008 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:5:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    5 |         scanf("%d%d", &n, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:6:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    6 |         for(int i = 0; i < n; i++) { scanf("%d", &a[i]); }
      |                                      ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>

int main() {
	int n, k, a[1000];
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++) { scanf("%d", &a[i]); }
	
	int c = 0;
	for(int l = 0; l < k; l++) {
		int m = (n - l + (k - 1)) / k;
		for(int j = m - 1; 0 <= j; j--) {
		for(int i = 0; i <= j - 1; i++) {
			int p = k * i + l;
			if(a[p] > a[p + k]) {
				int temp = a[p];
				a[p] = a[p + k];
				a[p + k] = temp;
				c++;
			}
		}
		}
	}
	
	for(int i = 0; i < n - 1; i++) {
		if(a[i] > a[i + 1]) { c = -1; break; }
	}
	
	printf("%d\n", c);
	
	return 0;
}
0