結果

問題 No.1247 ブロック登り
ユーザー ngtkanangtkana
提出日時 2020-07-01 23:13:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 622 ms / 3,000 ms
コード長 2,678 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 161,408 KB
実行使用メモリ 111,232 KB
最終ジャッジ日時 2024-04-17 03:10:53
合計ジャッジ時間 9,398 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 615 ms
111,104 KB
testcase_12 AC 570 ms
106,112 KB
testcase_13 AC 609 ms
111,232 KB
testcase_14 AC 605 ms
111,232 KB
testcase_15 AC 598 ms
111,232 KB
testcase_16 AC 610 ms
111,232 KB
testcase_17 AC 616 ms
111,232 KB
testcase_18 AC 563 ms
101,120 KB
testcase_19 AC 53 ms
18,560 KB
testcase_20 AC 17 ms
8,448 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 AC 7 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 622 ms
111,232 KB
testcase_26 AC 613 ms
111,232 KB
testcase_27 AC 610 ms
111,232 KB
testcase_28 AC 599 ms
111,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{convert::TryFrom, io::stdin};

trait OrdTools: Ord + Clone + Sized {
    fn chmin(&mut self, x: Self) {
        *self = std::cmp::min(self.clone(), x);
    }
    fn chmax(&mut self, x: Self) {
        *self = std::cmp::max(self.clone(), x);
    }
}

impl<T: Ord + Clone + Sized> OrdTools for T {}

fn main() {
    let (n, l) = {
        let mut s = String::new();
        stdin().read_line(&mut s).unwrap();
        let s = s.trim_end().to_owned();
        let mut s = s.split_whitespace();
        let n: usize = s.next().unwrap().parse().unwrap();
        let l: usize = s.next().unwrap().parse().unwrap();
        assert!(s.next().is_none());
        (n, l)
    };
    let a = {
        let mut s = String::new();
        stdin().read_line(&mut s).unwrap();
        let s = s.trim_end().to_owned();
        let mut s = s.split_whitespace();
        let mut a = vec![0; n];
        a.iter_mut()
            .for_each(|x| *x = s.next().unwrap().parse().unwrap());
        assert!(s.next().is_none());
        a
    };
    let mut ans = vec![std::i32::MIN; n];
    let mut dp = vec![vec![vec![std::i32::MIN; n]; n]; l + 1];

    (0..n).for_each(|j| dp[l][j][j] = l as i32 * a[j]);
    for i in (0..=l).rev() {
        if i + 3 <= l {
            let [crr, _, prv, _] = <&mut [_; 4]>::try_from(&mut dp[i..=i + 3]).unwrap();
            for j in 0..n {
                for k in 0..n {
                    crr[j][k].chmax(prv[j][k]);
                }
            }
        }
        let (first, last) = dp.split_at_mut(i);
        let crr = &last[0];
        for j in 0..n {
            let mut update = |ni: usize, nj: usize, nk: usize, k| {
                first[ni][nj][nk].chmax(crr[j][k] + ni as i32 * a[nj]);
            };
            for k in (0..=j).filter(|&k| crr[j][k] != std::i32::MIN) {
                let d = j - k;
                if d + 1 <= i && 0 < k {
                    update(i - d - 1, k - 1, j, k);
                }
                if 0 < i && j + 1 < n {
                    update(i - 1, j + 1, k, k);
                }
            }
            for k in (j..n).filter(|&k| crr[j][k] != std::i32::MIN) {
                let d = k - j;
                if 0 < i && 0 < j {
                    update(i - 1, j - 1, k, k);
                }
                if d + 1 <= i && k + 1 < n {
                    update(i - d - 1, k + 1, j, k);
                }
            }
        }
        (0..n.checked_sub(i).unwrap_or(0))
            .for_each(|j| (j + i..n).for_each(|k| ans[j + i].chmax(crr[j][k])));
        (i..n).for_each(|j| (0..=j - i).for_each(|k| ans[j - i].chmax(crr[j][k])));
    }

    ans.iter().for_each(|x| println!("{}", x));
}
0