結果

問題 No.366 ロボットソート
ユーザー YamaKasaYamaKasa
提出日時 2018-09-19 00:18:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 2,004 ms
コンパイル使用メモリ 73,888 KB
実行使用メモリ 56,736 KB
最終ジャッジ日時 2023-09-25 10:00:58
合計ジャッジ時間 6,858 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
56,212 KB
testcase_01 AC 118 ms
55,812 KB
testcase_02 AC 116 ms
56,220 KB
testcase_03 AC 115 ms
55,892 KB
testcase_04 AC 119 ms
55,568 KB
testcase_05 AC 117 ms
55,636 KB
testcase_06 AC 119 ms
56,180 KB
testcase_07 AC 118 ms
55,876 KB
testcase_08 AC 117 ms
56,076 KB
testcase_09 AC 116 ms
56,324 KB
testcase_10 AC 135 ms
56,048 KB
testcase_11 AC 182 ms
56,048 KB
testcase_12 AC 192 ms
56,736 KB
testcase_13 AC 172 ms
56,376 KB
testcase_14 AC 173 ms
56,392 KB
testcase_15 AC 139 ms
56,236 KB
testcase_16 AC 175 ms
56,148 KB
testcase_17 AC 179 ms
56,096 KB
testcase_18 AC 189 ms
56,376 KB
testcase_19 AC 179 ms
56,268 KB
testcase_20 AC 125 ms
56,092 KB
testcase_21 AC 182 ms
56,304 KB
testcase_22 AC 207 ms
56,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int cnt = 0;
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int K = scan.nextInt();
		int []a = new int[N];
		int []b = new int[N];
		for(int i = 0; i < N; i++) {
			a[i] = scan.nextInt();
			b[i] = a[i];
		}
		scan.close();
		Arrays.sort(b);
		for(int i = 0; i < K; i++) {
			kStepBubbleSort(a, N - i, K);
		}
		for(int i = 0; i < N; i++) {
			if(a[i] != b[i]) {
				System.out.println("-1");
				System.exit(0);
			}
		}
		System.out.println(cnt);
//		for(int i : a) {
//			System.out.print(i + " ");
//		}
//		System.out.println();
	}
	static void kStepBubbleSort(int[]A, int n, int K) {
		boolean flag = true;
		while(flag) {
			flag = false;
			for(int i = n - 1; i - K >= 0; i -= K) {
				if(A[i] < A[i - K]) {
					int t = A[i];
					A[i] = A[i - K];
					A[i - K] = t;
					flag = true;
					cnt ++;
				}
			}
		}
	}
}
0