結果
問題 |
No.527 ナップサック容量問題
|
ユーザー |
|
提出日時 | 2017-10-11 01:53:47 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 106 ms / 2,000 ms |
コード長 | 2,150 bytes |
コンパイル時間 | 27,773 ms |
コンパイル使用メモリ | 389,732 KB |
実行使用メモリ | 81,024 KB |
最終ジャッジ日時 | 2024-11-17 08:45:44 |
合計ジャッジ時間 | 31,274 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#[allow(unused_imports)] use std::cmp::{max, min}; #[allow(unused_imports)] use std::collections::{HashMap, HashSet}; mod util { use std::io::stdin; use std::str::FromStr; use std::fmt::Debug; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn get<T: FromStr>() -> T where <T as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() } #[allow(dead_code)] pub fn gets<T: FromStr>() -> Vec<T> where <T as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn get2<T: FromStr, U: FromStr>() -> (T, U) where <T as FromStr>::Err: Debug, <U as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } } fn main() { let n: usize = util::get(); let vw: Vec<(usize, usize)> = (0..n).map(|_| util::get2()).collect(); let v: usize = util::get(); let mut dp = vec![vec![0; 100000 + 2]; n + 1]; for i in 1..n + 1 { let vi = vw[i - 1].0; let wi = vw[i - 1].1; for k in 0..dp[i].len() { if k >= wi { dp[i][k] = max(dp[i][k], dp[i - 1][k - wi] + vi); } dp[i][k] = max(dp[i][k], dp[i - 1][k]); } } let w_min = (1..).find(|&i| dp[n][i] == v).unwrap(); let w_max = (1..dp[n].len()).filter(|&i| dp[n][i] == v).last().unwrap(); println!("{}", w_min); if w_max <= 100000 { println!("{}", w_max); } else { println!("inf"); } }