結果

問題 No.1028 闇討ち
ユーザー ks2mks2m
提出日時 2020-04-17 22:16:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 901 ms / 2,000 ms
コード長 1,493 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 78,252 KB
実行使用メモリ 95,804 KB
最終ジャッジ日時 2024-04-14 14:32:05
合計ジャッジ時間 12,764 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,016 KB
testcase_01 AC 52 ms
50,384 KB
testcase_02 AC 52 ms
50,352 KB
testcase_03 AC 52 ms
50,300 KB
testcase_04 AC 52 ms
50,476 KB
testcase_05 AC 56 ms
50,296 KB
testcase_06 AC 65 ms
50,780 KB
testcase_07 AC 54 ms
50,408 KB
testcase_08 AC 55 ms
50,164 KB
testcase_09 AC 141 ms
55,124 KB
testcase_10 AC 164 ms
55,544 KB
testcase_11 AC 302 ms
63,796 KB
testcase_12 AC 879 ms
94,928 KB
testcase_13 AC 848 ms
94,656 KB
testcase_14 AC 542 ms
75,412 KB
testcase_15 AC 262 ms
60,528 KB
testcase_16 AC 894 ms
95,060 KB
testcase_17 AC 886 ms
95,516 KB
testcase_18 AC 901 ms
95,532 KB
testcase_19 AC 891 ms
95,372 KB
testcase_20 AC 899 ms
94,884 KB
testcase_21 AC 900 ms
95,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		List<List<Obj>> list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>(n));
		}
		for (int i = 0; i < n; i++) {
			String[] sa = br.readLine().split(" ");
			for (int j = 0; j < n; j++) {
				int a = Integer.parseInt(sa[j]) - 1;
				Obj o = new Obj();
				o.x = i;
				o.y = j;
				list.get(a).add(o);
			}
		}
		br.close();

		long ans = 0;
		for (int i = 0; i < n; i++) {
			int[] cnt1 = new int[n];
			int[] cnt2 = new int[n];
			for (Obj o : list.get(i)) {
				int l = o.x - o.y - 1;
				if (l >= 0) {
					cnt1[l]++;
				}
				int r = o.x + o.y + 1;
				if (r < n) {
					cnt2[r]++;
				}
			}
			for (int j = n - 2; j >= 0; j--) {
				cnt1[j] += cnt1[j + 1];
			}
			for (int j = n - 2; j >= 0; j--) {
				cnt1[j] += cnt1[j + 1];
			}
			for (int j = 1; j < n; j++) {
				cnt2[j] += cnt2[j - 1];
			}
			for (int j = 1; j < n; j++) {
				cnt2[j] += cnt2[j - 1];
			}
			long min = Long.MAX_VALUE;
			for (int j = 0; j < n; j++) {
				int val = cnt1[j] + cnt2[j];
				min = Math.min(min, val);
			}
			ans += min;
		}
		for (int i = 1; i < n; i++) {
			ans += i * n;
		}
		System.out.println(ans);
	}

	static class Obj {
		int x, y;
	}
}
0