結果

問題 No.366 ロボットソート
ユーザー tentententen
提出日時 2020-11-18 09:33:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 953 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 72,044 KB
実行使用メモリ 56,652 KB
最終ジャッジ日時 2023-09-30 14:51:46
合計ジャッジ時間 6,687 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,636 KB
testcase_01 AC 120 ms
56,064 KB
testcase_02 AC 120 ms
55,644 KB
testcase_03 AC 119 ms
55,868 KB
testcase_04 AC 122 ms
55,748 KB
testcase_05 AC 124 ms
55,720 KB
testcase_06 AC 120 ms
55,664 KB
testcase_07 AC 119 ms
55,772 KB
testcase_08 AC 121 ms
55,732 KB
testcase_09 AC 120 ms
54,456 KB
testcase_10 AC 136 ms
55,804 KB
testcase_11 AC 179 ms
55,948 KB
testcase_12 AC 182 ms
56,520 KB
testcase_13 AC 169 ms
56,652 KB
testcase_14 AC 172 ms
56,112 KB
testcase_15 AC 157 ms
55,852 KB
testcase_16 AC 174 ms
56,500 KB
testcase_17 AC 170 ms
56,036 KB
testcase_18 AC 189 ms
56,256 KB
testcase_19 AC 184 ms
55,964 KB
testcase_20 AC 125 ms
55,896 KB
testcase_21 AC 197 ms
56,596 KB
testcase_22 AC 200 ms
56,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int count = 0;
        for (int i = 0; i < k; i++) {
            for (int j = n; i + k < j; j -= k) {
                for (int a = i; a + k < j; a += k) {
                    if (arr[a] > arr[a + k]) {
                        int tmp = arr[a];
                        arr[a] = arr[a + k];
                        arr[a + k] = tmp;
                        count++;
                    }
                }
            }
        }
        for (int i = 0; i < n - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                System.out.println(-1);
                return;
            }
        }
        System.out.println(count);
    }
}
0