結果
問題 | No.366 ロボットソート |
ユーザー |
![]() |
提出日時 | 2019-11-21 11:18:15 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 323 ms / 2,000 ms |
コード長 | 912 bytes |
コンパイル時間 | 2,358 ms |
コンパイル使用メモリ | 79,756 KB |
実行使用メモリ | 58,964 KB |
最終ジャッジ日時 | 2024-10-09 13:43:14 |
合計ジャッジ時間 | 7,109 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
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); } }