結果

問題 No.366 ロボットソート
ユーザー mitsuomitsuo
提出日時 2016-05-09 21:45:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 552 ms / 2,000 ms
コード長 1,391 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 75,404 KB
実行使用メモリ 65,960 KB
最終ジャッジ日時 2023-08-28 15:16:04
合計ジャッジ時間 8,463 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,572 KB
testcase_01 AC 115 ms
55,624 KB
testcase_02 AC 115 ms
55,512 KB
testcase_03 AC 114 ms
55,620 KB
testcase_04 AC 114 ms
56,452 KB
testcase_05 AC 116 ms
55,740 KB
testcase_06 AC 114 ms
55,884 KB
testcase_07 AC 114 ms
55,624 KB
testcase_08 AC 114 ms
55,856 KB
testcase_09 AC 115 ms
55,956 KB
testcase_10 AC 159 ms
57,872 KB
testcase_11 AC 482 ms
62,916 KB
testcase_12 AC 552 ms
65,560 KB
testcase_13 AC 247 ms
60,812 KB
testcase_14 AC 309 ms
60,528 KB
testcase_15 AC 173 ms
58,700 KB
testcase_16 AC 247 ms
60,360 KB
testcase_17 AC 305 ms
60,476 KB
testcase_18 AC 454 ms
65,828 KB
testcase_19 AC 289 ms
58,824 KB
testcase_20 AC 116 ms
55,864 KB
testcase_21 AC 356 ms
65,960 KB
testcase_22 AC 440 ms
65,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.fedom.challange.yuki.l2.q366;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<String> input = new ArrayList<>();
		while (sc.hasNext()) {
			input.add(sc.nextLine());
		}

		System.out.println(solve(input));

		sc.close();
	}

	public static int solve(List<String> in) {
		int N = Integer.valueOf(in.get(0).split(" ")[0]);
		int K = Integer.valueOf(in.get(0).split(" ")[1]);
		int[][] a = new int[N][];
		int count = 0;

		for (int i = 0; i < N; i++) {
			a[i] = new int[N];
		}

		for (int i = 0; i < N; i++) {
			int x = i % K;
			int y = i / K;
			a[x][y] = Integer.valueOf(in.get(1).split(" ")[i]);
//			System.out.println(x + ", " +y + "=" + a[x][y]);
		}
		for (int i = 0; i < N; i++) {
			count += sort(a[i]);
		}

		for (int i = 0; i < N - 1; i++) {
			int x = i % K;
			int y = i / K;
			int nx = (i + 1) % K;
			int ny = (i + 1) / K;
			if (a[nx][ny] < a[x][y] && a[x][y] > 0 && a[nx][ny] > 0) {
				return -1;
			}
		}

		return count;
	}

	private static int sort(int[] ar) {
		int c = 0;
		for (int i = 0; i < ar.length; i++) {
			for (int j = i + 1; j < ar.length; j++) {
				if (ar[i] > 0 && ar[j] > 0 && ar[j] < ar[i]) {
					int tmp = ar[j];
					ar[j] = ar[i];
					ar[i] = tmp;
					c++;
				}

			}
		}
		return c;
	}
}
0