結果
問題 |
No.45 回転寿司
|
ユーザー |
|
提出日時 | 2020-04-17 17:20:25 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,779 bytes |
コンパイル時間 | 13,179 ms |
コンパイル使用メモリ | 379,368 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-03 10:27:35 |
合計ジャッジ時間 | 15,191 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
use std::cmp; use std::io::{self, Read}; #[derive(Debug)] struct Input { n: i32, 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::<i32>().unwrap()) .collect(); Input { n, vs } } struct State { dp: [u32; 1000], } impl State { fn new() -> State { State { dp: [0; 1000] } } fn get(&self, i: i32) -> Option<u32> { self.as_index(i).map(|i| self.dp[i]) } fn get_or(&self, i: i32, default: u32) -> u32 { self.get(i).unwrap_or(default) } fn set(&mut self, i: i32, value: u32) { if let Some(i) = self.as_index(i) { self.dp[i] = value; } } fn as_index(&self, i: i32) -> Option<usize> { if i >= 0 && (i as usize) < self.dp.len() { Some(i as usize) } else { None } } } fn solve(input: Input, _cin_lock: &mut io::StdinLock) { let mut state = State::new(); for i in 0..input.n { let take = state.get_or(i - 2, 0) + input.vs[i as usize] as u32; let skip = state.get_or(i - 1, 0); state.set(i, cmp::max(take, skip)); } let answer = cmp::max(state.get_or(input.n - 1, 0), state.get_or(input.n, 0)); 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); }