結果
| 問題 |
No.45 回転寿司
|
| ユーザー |
|
| 提出日時 | 2020-10-02 16:59:13 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| コード長 | 1,271 bytes |
| コンパイル時間 | 10,968 ms |
| コンパイル使用メモリ | 400,364 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-08 12:20:14 |
| 合計ジャッジ時間 | 12,146 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
use std::cmp;
use std::io::{self, Read};
#[derive(Debug)]
struct Input {
n: usize,
vs: Vec<u32>,
}
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 max_value_of(input: Input) -> u32 {
if input.n == 1 {
return input.vs[0];
}
let mut dp = vec![0; input.n];
dp[0] = input.vs[0];
dp[1] = cmp::max(input.vs[0], input.vs.get(1).copied().unwrap_or(0));
for i in 2..input.n {
let take = dp[i - 2] + input.vs[i];
let skip = dp[i - 1];
dp[i] = cmp::max(take, skip);
}
dp[input.n - 1]
}
fn solve(input: Input, _cin_lock: &mut io::StdinLock) {
let answer = max_value_of(input);
println!("{}", answer);
}
fn main() {
let cin = io::stdin();
let mut cin_lock = cin.lock();
let input = read_input(&mut cin_lock);
solve(input, &mut cin_lock);
}