結果

問題 No.527 ナップサック容量問題
ユーザー hatoohatoo
提出日時 2017-10-11 01:53:47
言語 Rust
(1.77.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,150 bytes
コンパイル時間 15,655 ms
コンパイル使用メモリ 389,652 KB
実行使用メモリ 81,024 KB
最終ジャッジ日時 2024-04-28 15:34:09
合計ジャッジ時間 17,341 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 5 ms
6,816 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 38 ms
34,048 KB
testcase_06 AC 79 ms
69,248 KB
testcase_07 AC 67 ms
55,936 KB
testcase_08 AC 33 ms
29,312 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 81 ms
70,144 KB
testcase_11 AC 57 ms
49,664 KB
testcase_12 AC 41 ms
36,352 KB
testcase_13 AC 84 ms
71,680 KB
testcase_14 AC 31 ms
27,008 KB
testcase_15 AC 49 ms
41,856 KB
testcase_16 AC 6 ms
6,944 KB
testcase_17 AC 39 ms
34,816 KB
testcase_18 AC 44 ms
39,424 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 32 ms
28,544 KB
testcase_21 AC 95 ms
81,024 KB
testcase_22 AC 63 ms
54,400 KB
testcase_23 AC 92 ms
78,720 KB
testcase_24 AC 19 ms
17,536 KB
testcase_25 AC 60 ms
52,992 KB
testcase_26 AC 57 ms
47,232 KB
testcase_27 AC 55 ms
48,128 KB
testcase_28 AC 48 ms
41,856 KB
testcase_29 AC 94 ms
80,256 KB
testcase_30 AC 10 ms
9,728 KB
testcase_31 AC 39 ms
35,584 KB
testcase_32 AC 50 ms
43,392 KB
testcase_33 AC 43 ms
37,120 KB
testcase_34 AC 52 ms
44,928 KB
testcase_35 AC 53 ms
44,928 KB
testcase_36 AC 5 ms
6,944 KB
testcase_37 AC 40 ms
33,920 KB
testcase_38 AC 55 ms
42,624 KB
testcase_39 AC 45 ms
39,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[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");
    }

}
0