結果

問題 No.595 登山
ユーザー hatoohatoo
提出日時 2017-11-11 00:06:44
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,722 bytes
コンパイル時間 13,720 ms
コンパイル使用メモリ 377,404 KB
実行使用メモリ 14,492 KB
最終ジャッジ日時 2024-05-03 14:57:54
合計ジャッジ時間 15,617 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 15 ms
8,224 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 29 ms
14,312 KB
testcase_27 AC 26 ms
14,340 KB
testcase_28 AC 27 ms
14,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, BinaryHeap};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

#[allow(unused_macros)]
macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
                   ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
                   ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn main() {
    let (n, p) = get!(usize, usize);
    let h = get!(usize;;);

    let mut dp = vec![vec![usize::max_value() / 4; 2]; n];
    dp[0][0] = 0;
    dp[0][1] = 0;

    for i in 1..n {
        let d = if h[i] <= h[i - 1] { 0 } else { h[i] - h[i - 1] };
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][0] + p);
        dp[i][0] = min(dp[i][0], dp[i - 1][1] + p);

        dp[i][1] = dp[i][0];

        let mut k = i - 1;
        while h[k] <= h[k + 1] && k > 0 {
            dp[i][1] = min(dp[i][1], dp[k - 1][0] + p);
            dp[i][1] = min(dp[i][1], dp[k - 1][1] + p);
            k -= 1;
        }
    }

    println!("{}", dp[n - 1].iter().min().unwrap());
}
0