結果

問題 No.1247 ブロック登り
ユーザー akakimidoriakakimidori
提出日時 2021-12-20 12:32:43
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,496 ms / 3,000 ms
コード長 4,238 bytes
コンパイル時間 4,657 ms
コンパイル使用メモリ 153,640 KB
実行使用メモリ 430,164 KB
最終ジャッジ日時 2023-10-13 19:30:06
合計ジャッジ時間 23,178 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 5 ms
4,784 KB
testcase_08 AC 7 ms
5,056 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 1,339 ms
430,124 KB
testcase_12 AC 1,229 ms
410,356 KB
testcase_13 AC 1,409 ms
430,156 KB
testcase_14 AC 1,389 ms
430,152 KB
testcase_15 AC 1,315 ms
430,116 KB
testcase_16 AC 1,351 ms
430,148 KB
testcase_17 AC 1,394 ms
430,116 KB
testcase_18 AC 1,164 ms
391,608 KB
testcase_19 AC 104 ms
68,472 KB
testcase_20 AC 52 ms
28,876 KB
testcase_21 AC 27 ms
6,176 KB
testcase_22 AC 17 ms
9,592 KB
testcase_23 AC 13 ms
7,820 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1,363 ms
430,160 KB
testcase_26 AC 1,341 ms
430,164 KB
testcase_27 AC 1,380 ms
430,160 KB
testcase_28 AC 1,496 ms
430,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

fn run() {
    input! {
        n: usize,
        k: usize,
        a: [i64; n],
    }
    let inf = 10000 * 300 * 300 * 2i64;
    let mut dp = vec![vec![vec![(-inf, -inf); n]; n]; k + 1];
    for (i, a) in a.iter().enumerate() {
        let v = *a * k as i64;
        dp[k][i][i] = (v, v);
    }
    let mut left = vec![vec![-inf; n]; n];
    let mut right = vec![vec![-inf; n]; n];
    for i in (0..=k).rev() {
        for l in 0..n {
            for r in l..n {
                let p = dp[i][l][r];
                if p.0 == -inf {
                    continue;
                }
                if l < r && i >= 2 {
                    dp[i - 2][l][r].0.chmax(p.0);
                    dp[i - 2][l][r].1.chmax(p.1);
                }
                if l < r {
                    if i % 2 == 0 {
                        left[l][r.min(l + i)].chmax(p.0);
                        right[r][l.max(r.saturating_sub(i))].chmax(p.1);
                    } else {
                        if l + 1 < n {
                            left[l + 1][r.min(l + i)].chmax(p.0);
                        }
                        if r > 0 {
                            right[r - 1][l.max(r.saturating_sub(i))].chmax(p.1);
                        }
                    }
                }
                if i >= r - l {
                    let x = i - (r - l);
                    dp[x][l][r].1.chmax(p.0);
                    dp[x][l][r].0.chmax(p.1);
                }
                if l > 0 && i > 0 {
                    let x = i - 1;
                    dp[x][l - 1][r].0.chmax(p.0 + x as i64 * a[l - 1]);
                }
                if r + 1 < n && i > 0 {
                    let x = i - 1;
                    dp[x][l][r + 1].1.chmax(p.1 + x as i64 * a[r + 1]);
                }
            }
        }
    }
    let mut ans = vec![-inf; n];
    for (l, left) in left.iter().enumerate() {
        for (r, v) in left.iter().enumerate() {
            for i in (l..=r).step_by(2) {
                ans[i].chmax(*v);
            }
        }
    }
    for (r, right) in right.iter().enumerate() {
        for (l, v) in right.iter().enumerate() {
            for i in (l..=r).rev().step_by(2) {
                ans[i].chmax(*v);
            }
        }
    }
    for a in ans {
        println!("{}", a);
    }
}

fn main() {
    run();
}
0