結果

問題 No.1028 闇討ち
ユーザー phsplsphspls
提出日時 2023-01-03 21:25:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,497 bytes
コンパイル時間 11,046 ms
コンパイル使用メモリ 377,268 KB
実行使用メモリ 33,536 KB
最終ジャッジ日時 2024-05-05 07:37:18
合計ジャッジ時間 13,748 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 14 ms
9,216 KB
testcase_12 AC 62 ms
31,872 KB
testcase_13 AC 60 ms
31,744 KB
testcase_14 AC 34 ms
19,712 KB
testcase_15 AC 12 ms
7,680 KB
testcase_16 AC 78 ms
33,536 KB
testcase_17 AC 64 ms
33,408 KB
testcase_18 AC 63 ms
33,536 KB
testcase_19 AC 67 ms
33,536 KB
testcase_20 AC 65 ms
33,536 KB
testcase_21 AC 68 ms
33,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let grid = (0..n).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse::<usize>().unwrap()-1).collect();
            temp
        })
        .collect::<Vec<_>>();

    let mut totals = vec![0usize; n];
    let mut ldists = vec![vec![0usize; n]; n];
    let mut rdists = vec![vec![0usize; n]; n];
    for i in 0..n {
        for j in 0..n {
            totals[grid[i][j]] += j;
            if i > j {
                ldists[grid[i][j]][i-j-1] += 1;
            }
            if i+j+1 < n {
                rdists[grid[i][j]][i+j+1] += 1;
            }
        }
    }
    let mut dists = vec![vec![0usize; n]; n];
    for i in 0..n {
        let mut incval = rdists[i][0];
        for j in 0..n-1 {
            let prev = incval;
            incval += rdists[i][j+1];
            rdists[i][j+1] += rdists[i][j] + prev;
        }
        let mut incval = ldists[i][n-1];
        for j in (1..n).rev() {
            let prev = incval;
            incval += ldists[i][j-1];
            ldists[i][j-1] += ldists[i][j] + prev;
        }
        for j in 0..n {
            dists[i][j] = totals[i] + ldists[i][j] + rdists[i][j];
        }
    }
    println!("{}", dists.iter().map(|v| v.iter().min().unwrap()).sum::<usize>());
}
0