結果

問題 No.1077 Noelちゃんと星々4
ユーザー phsplsphspls
提出日時 2022-12-29 23:26:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 925 bytes
コンパイル時間 1,237 ms
コンパイル使用メモリ 145,720 KB
実行使用メモリ 80,076 KB
最終ジャッジ日時 2023-08-16 10:08:18
合計ジャッジ時間 3,783 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 60 ms
55,488 KB
testcase_03 AC 78 ms
71,296 KB
testcase_04 AC 24 ms
22,784 KB
testcase_05 AC 18 ms
17,512 KB
testcase_06 AC 30 ms
28,816 KB
testcase_07 AC 35 ms
33,096 KB
testcase_08 AC 26 ms
24,600 KB
testcase_09 AC 22 ms
21,432 KB
testcase_10 AC 78 ms
71,364 KB
testcase_11 AC 50 ms
46,816 KB
testcase_12 AC 42 ms
38,888 KB
testcase_13 AC 17 ms
16,736 KB
testcase_14 AC 77 ms
69,784 KB
testcase_15 AC 40 ms
37,568 KB
testcase_16 AC 27 ms
25,176 KB
testcase_17 AC 46 ms
42,328 KB
testcase_18 AC 79 ms
72,684 KB
testcase_19 AC 16 ms
15,924 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 88 ms
80,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF: isize = 1isize << 60;
const LIMIT: usize = 1e4 as usize;

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut y = String::new();
    std::io::stdin().read_line(&mut y).ok();
    let y: Vec<isize> = y.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut dp = vec![vec![INF; LIMIT+1]; n];
    for i in 0..=LIMIT {
        dp[0][i] = (y[0] - i as isize).abs();
    }
    for i in 1..n {
        let mut mincost = INF;
        for j in 0..=LIMIT {
            let diff = (j as isize - y[i]).abs();
            mincost = mincost.min(diff + dp[i-1][j]);
            dp[i][j] = dp[i][j].min(mincost);
            if (j as isize) < y[i] {
                mincost -= 1;
            } else {
                mincost += 1;
            }
        }
    }
    println!("{}", dp[n-1].iter().min().unwrap());
}
0