結果
| 問題 | No.527 ナップサック容量問題 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-11 02:13:05 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 140 ms / 2,000 ms |
| コード長 | 2,212 bytes |
| 記録 | |
| コンパイル時間 | 22,089 ms |
| コンパイル使用メモリ | 388,924 KB |
| 実行使用メモリ | 81,024 KB |
| 最終ジャッジ日時 | 2024-11-17 08:48:50 |
| 合計ジャッジ時間 | 19,804 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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 inf = 1e9 as usize;
let mut dp = vec![vec![inf as usize; 100000 + 2]; n + 1];
dp[0][0] = 0;
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()).rev() {
if k + 1 < dp[i].len() {
dp[i][k] = min(dp[i][k], dp[i][k + 1]);
}
if k >= vi {
dp[i][k] = min(dp[i][k], dp[i - 1][k - vi] + wi);
}
dp[i][k] = min(dp[i][k], dp[i - 1][k]);
}
}
println!("{}", max(1, dp[n][v]));
if dp[n][v + 1] != inf {
println!("{}", dp[n][v + 1] - 1);
} else {
println!("inf");
}
}