結果

問題 No.1077 Noelちゃんと星々4
ユーザー phsplsphspls
提出日時 2022-12-29 01:59:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 871 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 143,756 KB
実行使用メモリ 80,088 KB
最終ジャッジ日時 2023-08-15 19:55:50
合計ジャッジ時間 2,795 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 56 ms
55,428 KB
testcase_03 AC 71 ms
71,356 KB
testcase_04 AC 20 ms
22,780 KB
testcase_05 AC 16 ms
17,412 KB
testcase_06 AC 26 ms
28,836 KB
testcase_07 AC 31 ms
33,056 KB
testcase_08 AC 22 ms
24,636 KB
testcase_09 AC 19 ms
21,448 KB
testcase_10 AC 69 ms
71,352 KB
testcase_11 AC 45 ms
46,824 KB
testcase_12 AC 37 ms
38,872 KB
testcase_13 AC 15 ms
16,724 KB
testcase_14 AC 68 ms
69,756 KB
testcase_15 AC 35 ms
37,572 KB
testcase_16 AC 23 ms
25,168 KB
testcase_17 AC 39 ms
42,268 KB
testcase_18 AC 71 ms
72,632 KB
testcase_19 AC 14 ms
15,828 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 80 ms
80,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF: isize = 1isize << 60;
const LIMIT: usize = 10000;

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] = (i as isize - y[0]).abs();
    }
    for i in 1..n {
        let mut cost = INF;
        for j in 0..=LIMIT {
            cost = cost.min(dp[i-1][j] + (y[i] - j as isize).abs());
            dp[i][j] = dp[i][j].min(cost);
            if (j as isize) < y[i] {
                cost -= 1;
            } else {
                cost += 1;
            }
        }
    }
    println!("{}", dp[n-1].iter().min().unwrap());
}
0