結果

問題 No.366 ロボットソート
ユーザー kenji_shioyakenji_shioya
提出日時 2016-05-27 17:58:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 3,257 ms
コンパイル使用メモリ 73,648 KB
実行使用メモリ 56,440 KB
最終ジャッジ日時 2023-08-28 15:17:22
合計ジャッジ時間 7,648 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
54,336 KB
testcase_01 AC 120 ms
55,908 KB
testcase_02 AC 120 ms
55,668 KB
testcase_03 AC 121 ms
55,576 KB
testcase_04 AC 121 ms
56,024 KB
testcase_05 AC 123 ms
55,760 KB
testcase_06 AC 122 ms
55,592 KB
testcase_07 AC 121 ms
55,948 KB
testcase_08 AC 123 ms
55,632 KB
testcase_09 AC 123 ms
55,828 KB
testcase_10 AC 138 ms
55,560 KB
testcase_11 AC 185 ms
56,168 KB
testcase_12 AC 187 ms
55,688 KB
testcase_13 AC 175 ms
55,760 KB
testcase_14 AC 179 ms
56,188 KB
testcase_15 AC 157 ms
56,076 KB
testcase_16 AC 175 ms
55,900 KB
testcase_17 AC 171 ms
55,916 KB
testcase_18 AC 190 ms
55,716 KB
testcase_19 AC 180 ms
56,216 KB
testcase_20 AC 121 ms
55,668 KB
testcase_21 AC 196 ms
55,876 KB
testcase_22 AC 199 ms
56,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercises22{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    int size = sc.nextInt();
    int armWidth = sc.nextInt();

    int[] array = new int[size];

    for (int n = 0; n < size; n++){
      array[n] = sc.nextInt();
    }

    int count = 0;
    boolean flag = true;

    for (int a = 0; a < armWidth; a++){
      for (int k = a; k < getLastIndex(a, size, armWidth); k += armWidth){
        for(int i = getLastIndex(a, size, armWidth); k < i; i -= armWidth){
          if (array[i] < array[i - armWidth]){
            int w = array[i];
            array[i] = array[i - armWidth];
            array[i - armWidth] = w;
            count++;
          }
        }
      }
    }

    for (int n = 1; n < size; n++){
      if (array[n - 1] <= array[n]){
        flag = true;

      }else{
        flag = false;
        break;
      }
    }

    if(flag == true){
      System.out.println(count);
    }else{
      System.out.println(-1);
    }
  }

  private static int getLastIndex(int a, int size, int armWidth){
    int extr = 0;
    if (size % armWidth > a){
      extr = 1;
    }
    return (size / armWidth - 1 + extr) * armWidth + a;
  }
}
0