結果

問題 No.366 ロボットソート
ユーザー 37zigen37zigen
提出日時 2016-04-30 00:37:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 835 bytes
コンパイル時間 1,937 ms
コンパイル使用メモリ 79,172 KB
実行使用メモリ 56,944 KB
最終ジャッジ日時 2023-08-28 15:13:27
合計ジャッジ時間 6,276 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
56,196 KB
testcase_01 AC 116 ms
55,940 KB
testcase_02 AC 118 ms
55,852 KB
testcase_03 AC 118 ms
55,636 KB
testcase_04 AC 116 ms
56,200 KB
testcase_05 AC 116 ms
54,184 KB
testcase_06 AC 116 ms
55,896 KB
testcase_07 AC 117 ms
56,204 KB
testcase_08 AC 116 ms
55,872 KB
testcase_09 AC 117 ms
55,920 KB
testcase_10 AC 133 ms
55,860 KB
testcase_11 AC 180 ms
56,124 KB
testcase_12 AC 182 ms
56,136 KB
testcase_13 AC 167 ms
56,148 KB
testcase_14 AC 170 ms
56,576 KB
testcase_15 AC 156 ms
56,360 KB
testcase_16 AC 171 ms
55,928 KB
testcase_17 AC 174 ms
56,012 KB
testcase_18 AC 175 ms
56,444 KB
testcase_19 AC 178 ms
56,572 KB
testcase_20 AC 120 ms
55,788 KB
testcase_21 AC 192 ms
56,300 KB
testcase_22 AC 199 ms
56,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		long[] a=new long[n];
		for(int i=0;i<n;i++){
			a[i]=sc.nextLong();
		}
		int count=0;
		for(int i=0;i<n;i++){
			long min=999999999;
			int m=-1;
			for(int j=0;i+j*k<n;j++){
				if(min>a[i+j*k]){
					m=i+j*k;
					min=a[i+j*k];
				}
			}
			for(int j=0;m-(j+1)*k>=i;j++){
				long d=a[m-j*k];
				a[m-j*k]=a[m-(j+1)*k];
				a[m-(j+1)*k]=d;
				count++;
			}
		}
		for(int i=0;i+1<n;i++){
			if(a[i]>a[i+1]){
				System.out.println(-1);
				return;
			}
		}
		System.out.println(count);
		
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}

}
0