結果

問題 No.366 ロボットソート
ユーザー jp_stejp_ste
提出日時 2016-05-10 09:55:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 2,307 ms
コンパイル使用メモリ 74,368 KB
実行使用メモリ 56,536 KB
最終ジャッジ日時 2023-08-28 15:16:16
合計ジャッジ時間 6,185 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,852 KB
testcase_01 AC 118 ms
55,840 KB
testcase_02 AC 117 ms
55,568 KB
testcase_03 AC 116 ms
56,268 KB
testcase_04 AC 120 ms
56,212 KB
testcase_05 AC 117 ms
56,344 KB
testcase_06 AC 117 ms
55,896 KB
testcase_07 AC 117 ms
56,060 KB
testcase_08 AC 117 ms
56,536 KB
testcase_09 AC 119 ms
56,212 KB
testcase_10 AC 136 ms
55,620 KB
testcase_11 AC 170 ms
56,180 KB
testcase_12 AC 183 ms
56,204 KB
testcase_13 AC 168 ms
56,028 KB
testcase_14 AC 172 ms
56,316 KB
testcase_15 AC 149 ms
55,604 KB
testcase_16 AC 171 ms
56,080 KB
testcase_17 AC 173 ms
56,316 KB
testcase_18 AC 188 ms
56,392 KB
testcase_19 AC 182 ms
56,352 KB
testcase_20 AC 119 ms
56,196 KB
testcase_21 AC 194 ms
56,168 KB
testcase_22 AC 198 ms
56,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int n = scan.nextInt();
            int k = scan.nextInt();
            int list[] = new int[n];
            for(int i=0; i<n; i++) {
                list[i] = scan.nextInt();
            }
            
            int ans = 0;
            int m = n;
            while(m > 0) {
                for(int g=0; g<k; g++) {
                    for(int p=g; p+k<m; p+=k) {
                        if(list[p] > list[p+k]) {
                            int w = list[p];
                            list[p] = list[p+k];
                            list[p+k] = w;
                            ans++;
                        }
                    }
                }
                m -= k;
            }
                        
            boolean flag = true; 
            for(int i=0; i<n-1; i++) {
                if(list[i] > list[i+1]) {
                    flag = false;
                    break;
                }
            }
            System.out.println(flag? ans : -1);
        }
    }
}
0