結果

問題 No.595 登山
ユーザー koba-e964koba-e964
提出日時 2021-11-15 00:47:13
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 2,203 bytes
コンパイル時間 2,696 ms
コンパイル使用メモリ 153,956 KB
実行使用メモリ 8,300 KB
最終ジャッジ日時 2023-08-20 10:29:02
合計ジャッジ時間 4,139 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    input! {
        n: usize, p: i64,
        h: [i64; n],
    }
    let mut acc_l = vec![0; n];
    for i in 0..n - 1 {
        acc_l[i + 1] = acc_l[i] + max(0, h[i + 1] - h[i]);
    }
    let mut acc_r = vec![0; n];
    for i in (0..n - 1).rev() {
        acc_r[i] = acc_r[i + 1] + max(0, h[i] - h[i + 1]);
    }
    let mut dp = vec![0; n + 1];
    let mut mim = 0;
    let mut mip = acc_r[0];
    for j in 1..n + 1 {
        dp[j] = min(mim + acc_l[j - 1], mip - acc_r[j - 1]) + p;
        mim = min(mim, dp[j] - acc_l[j]);
        mip = min(mip, dp[j] + acc_r[j]);
    }
    println!("{}", dp[n] - p);
}
0