結果

問題 No.1028 闇討ち
ユーザー phsplsphspls
提出日時 2023-01-03 21:25:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,497 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 146,304 KB
実行使用メモリ 33,588 KB
最終ジャッジ日時 2023-08-18 00:46:54
合計ジャッジ時間 4,073 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 17 ms
9,320 KB
testcase_12 AC 76 ms
32,004 KB
testcase_13 AC 75 ms
31,736 KB
testcase_14 AC 40 ms
19,796 KB
testcase_15 AC 13 ms
7,468 KB
testcase_16 AC 79 ms
33,512 KB
testcase_17 AC 80 ms
33,584 KB
testcase_18 AC 80 ms
33,536 KB
testcase_19 AC 79 ms
33,504 KB
testcase_20 AC 78 ms
33,512 KB
testcase_21 AC 79 ms
33,588 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