結果

問題 No.366 ロボットソート
ユーザー akakimidoriakakimidori
提出日時 2017-04-27 15:00:10
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 606 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 24,956 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 16:03:21
合計ジャッジ時間 1,478 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>

void swap(int *a,int i,int j){
  int t=a[i];
  a[i]=a[j];
  a[j]=t;
  return;
}

void run(void){
  int n,k;
  scanf("%d%d",&n,&k);
  int *a=(int *)malloc(sizeof(int)*n);
  int i;
  for(i=0;i<n;i++){
    scanf("%d",a+i);
  }

  int count=0;
  int flag=1;
  while(flag){
    flag=0;
    for(i=0;i<n-k;i++){
      if(a[i]>a[i+k]){
	swap(a,i,i+k);
	count++;
	flag=1;
      }
    }
  }

  int can=1;
  for(i=0;i<n-1;i++){
    if(a[i]>a[i+1]){
      can=0;
      break;
    }
  }
  printf("%d\n",can?count:-1);
  free(a);
  return;
}

int main(void){
  run();
  return 0;
}
0