結果

問題 No.1247 ブロック登り
ユーザー ngtkanangtkana
提出日時 2020-07-01 23:16:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,101 ms / 3,000 ms
コード長 2,695 bytes
コンパイル時間 1,219 ms
コンパイル使用メモリ 159,944 KB
実行使用メモリ 111,232 KB
最終ジャッジ日時 2024-04-17 03:11:19
合計ジャッジ時間 12,165 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 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 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 733 ms
111,232 KB
testcase_12 AC 701 ms
106,112 KB
testcase_13 AC 756 ms
111,104 KB
testcase_14 AC 797 ms
111,232 KB
testcase_15 AC 768 ms
111,232 KB
testcase_16 AC 769 ms
111,232 KB
testcase_17 AC 754 ms
111,232 KB
testcase_18 AC 656 ms
101,120 KB
testcase_19 AC 56 ms
18,432 KB
testcase_20 AC 20 ms
8,448 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 751 ms
111,232 KB
testcase_26 AC 742 ms
111,232 KB
testcase_27 AC 1,101 ms
111,232 KB
testcase_28 AC 1,011 ms
111,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::convert::TryFrom;

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 stdin = std::io::stdin();
    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