結果
問題 |
No.45 回転寿司
|
ユーザー |
|
提出日時 | 2020-05-29 16:34:27 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,487 bytes |
コンパイル時間 | 13,471 ms |
コンパイル使用メモリ | 377,068 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-15 13:42:42 |
合計ジャッジ時間 | 15,083 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
use std::cmp; use std::io::{self, Read}; #[derive(Debug)] struct Input { n: usize, vs: Vec<i32>, } fn next_token(cin_lock: &mut io::StdinLock) -> String { cin_lock .by_ref() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>() } fn read_input(cin_lock: &mut io::StdinLock) -> Input { let n = next_token(cin_lock).parse().unwrap(); let vs = (0..n).map(|_| next_token(cin_lock).parse().unwrap()).collect(); Input { n, vs, } } fn solve(input: Input, _cin_lock: &mut io::StdinLock) { let answer = solve_dp(input); println!("{}", answer); } fn solve_dp(input: Input) -> i32 { if input.n <= 2 { let first = *input.vs.get(0).unwrap(); let second = input.vs.get(1).map(|x| *x).unwrap_or(0); return cmp::max(first, second); } let mut dp = vec![0; input.n]; dp[0] = input.vs[0]; dp[1] = cmp::max(input.vs[0], input.vs[1]); for (i, v) in input.vs.into_iter().enumerate().skip(2) { let take = *dp.get(i - 2).unwrap() + v; let skip = *dp.get(i - 1).unwrap(); dp[i] = cmp::max(take, skip); } let mut it = dp.into_iter().rev(); cmp::max(it.next().unwrap(), it.next().unwrap()) } fn main() { let cin = io::stdin(); let mut cin_lock = cin.lock(); let input = read_input(&mut cin_lock); solve(input, &mut cin_lock); }