結果

問題 No.366 ロボットソート
ユーザー htensaihtensai
提出日時 2019-11-21 11:18:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 912 bytes
コンパイル時間 1,950 ms
コンパイル使用メモリ 79,700 KB
実行使用メモリ 58,588 KB
最終ジャッジ日時 2024-04-17 19:39:58
合計ジャッジ時間 6,167 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
53,332 KB
testcase_01 AC 105 ms
53,164 KB
testcase_02 AC 105 ms
52,884 KB
testcase_03 AC 112 ms
53,672 KB
testcase_04 AC 116 ms
54,288 KB
testcase_05 AC 117 ms
54,024 KB
testcase_06 AC 115 ms
54,144 KB
testcase_07 AC 109 ms
52,960 KB
testcase_08 AC 106 ms
53,192 KB
testcase_09 AC 119 ms
53,988 KB
testcase_10 AC 131 ms
54,068 KB
testcase_11 AC 170 ms
54,580 KB
testcase_12 AC 178 ms
54,628 KB
testcase_13 AC 151 ms
54,372 KB
testcase_14 AC 163 ms
54,524 KB
testcase_15 AC 151 ms
54,352 KB
testcase_16 AC 164 ms
54,132 KB
testcase_17 AC 169 ms
54,760 KB
testcase_18 AC 187 ms
56,684 KB
testcase_19 AC 177 ms
54,588 KB
testcase_20 AC 106 ms
53,024 KB
testcase_21 AC 171 ms
54,512 KB
testcase_22 AC 295 ms
58,588 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];
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < n; i++) {
		    arr[i] = sc.nextInt();
		    map.put(arr[i], i);
		}
		int[] sorted = (int[])(arr.clone());
		Arrays.sort(sorted);
		int count = 0;
		for (int i = 0; i < n - 1; i++) {
		    int idx = map.get(sorted[i]);
		    if (i == idx) {
		        continue;
		    }
		    if ((idx - i) % k != 0) {
		        System.out.println(-1);
		        return;
		    }
		    while (i < idx) {
		        int tmp = arr[idx - k];
		        arr[idx - k] = arr[idx];
		        arr[idx] = tmp;
		        map.put(sorted[i], idx - k);
		        map.put(arr[idx], idx);
		        idx -= k;
		        count++;
		    }
		}
	    System.out.println(count);
	}
}
0