結果

問題 No.1028 闇討ち
ユーザー face4face4
提出日時 2020-08-06 11:28:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,364 ms / 2,000 ms
コード長 1,944 bytes
コンパイル時間 2,924 ms
コンパイル使用メモリ 78,484 KB
実行使用メモリ 174,380 KB
最終ジャッジ日時 2023-10-19 15:23:20
合計ジャッジ時間 18,368 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,624 KB
testcase_01 AC 55 ms
53,692 KB
testcase_02 AC 54 ms
53,576 KB
testcase_03 AC 53 ms
53,696 KB
testcase_04 AC 54 ms
53,688 KB
testcase_05 AC 59 ms
53,716 KB
testcase_06 AC 69 ms
54,556 KB
testcase_07 AC 54 ms
53,640 KB
testcase_08 AC 58 ms
53,712 KB
testcase_09 AC 179 ms
61,360 KB
testcase_10 AC 211 ms
63,800 KB
testcase_11 AC 517 ms
87,180 KB
testcase_12 AC 1,289 ms
166,960 KB
testcase_13 AC 1,317 ms
166,680 KB
testcase_14 AC 912 ms
114,416 KB
testcase_15 AC 413 ms
78,224 KB
testcase_16 AC 1,352 ms
170,028 KB
testcase_17 AC 1,343 ms
170,028 KB
testcase_18 AC 1,352 ms
170,236 KB
testcase_19 AC 1,364 ms
170,152 KB
testcase_20 AC 1,354 ms
174,380 KB
testcase_21 AC 1,356 ms
170,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> arr[][] = new ArrayList[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                arr[i][j] = new ArrayList<Integer>();
            }
        }
        for(int i = 0; i < n; i++){
            String[] line = br.readLine().split(" ");
            for(int j = 0; j < n; j++){
                arr[Integer.parseInt(line[j])-1][i].add(j);
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++){
            int sum[] = new int[n];
            for(int j = 0; j < n; j++){
                for(int k : arr[i][j]){
                    sum[j] += k;
                }
            }
            int down[] = new int[n], tmp[] = new int[n];
            int val = 0, acc = 0;
            for(int j = 0; j < n; j++){
                for(int k : arr[i][j]){
                    if(j+k+1 < n)   tmp[j+k+1]++;
                }
                val += sum[j];
                acc += tmp[j];
                val += acc;
                down[j] = val;
            }
            int up[] = new int[n];
            Arrays.fill(tmp, 0);
            val = 0;
            acc = 0;
            for(int j = n-1; j >= 0; j--){
                for(int k : arr[i][j]){
                    if(j-k-1 >= 0)  tmp[j-k-1]++;
                }
                val += sum[j];
                acc += tmp[j];
                val += acc;
                up[j] = val;
            }
            int score = 1<<30;
            for(int j = 0; j < n; j++){
                score = Math.min(score, down[j]+up[j]-sum[j]);
            }
            ans += score;
        }
        System.out.println(ans);
    }
}
0