結果
| 問題 |
No.45 回転寿司
|
| ユーザー |
sino
|
| 提出日時 | 2020-03-22 18:31:32 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| コード長 | 1,807 bytes |
| コンパイル時間 | 18,375 ms |
| コンパイル使用メモリ | 377,796 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-25 16:04:55 |
| 合計ジャッジ時間 | 19,529 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::collections::VecDeque;
#[allow(unused_macros)]
macro_rules! read {
([$t:ty] ; $n:expr) =>
((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
($($t:ty),+ ; $n:expr) =>
((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
([$t:ty]) =>
(rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
($t:ty) =>
(rl().parse::<$t>().unwrap());
($($t:ty),*) => {{
let buf = rl();
let mut w = buf.split_whitespace();
($(w.next().unwrap().parse::<$t>().unwrap()),*)
}};
}
#[allow(dead_code)]
fn rl() -> String {
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
buf.trim_end().to_owned()
}
trait IteratorExt: Iterator + Sized {
fn vec(self) -> Vec<Self::Item> {
self.collect()
}
}
impl<T: Iterator> IteratorExt for T {}
fn main() {
let _ = read!(usize; 1);
let v = read!([usize]);
let mut table = Vec::<Option<usize>>::with_capacity(v.len());
for _ in 0..v.len() {
table.push(None);
}
println!("{}", max_score(&v, 0, &mut table));
}
fn max_score(v: &Vec<usize>, i: usize, table: &mut Vec<Option<usize>>) -> usize {
if v.len() == i {return 0;}
if i == v.len() -1 {
table[i] = Some(v[i]);
return v[i];
}
let a = {
if v.len()-1 < i+2 {
v[i]
}
else {
if let Some(s) = table[i+2] {
v[i] + s
}
else {
v[i] + max_score(&v, i+2, table)
}
}
};
let b = if let Some(s) = table[i+1] {
s
}
else {
max_score(&v, i+1, table)
};
let ans = a.max(b);
table[i] = Some(ans);
ans
}
sino