結果

問題 No.527 ナップサック容量問題
ユーザー hatoohatoo
提出日時 2017-10-11 02:13:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 16,184 ms
コンパイル使用メモリ 400,616 KB
実行使用メモリ 81,024 KB
最終ジャッジ日時 2024-04-28 15:34:58
合計ジャッジ時間 20,034 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,816 KB
testcase_01 AC 6 ms
6,940 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 6 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 52 ms
34,048 KB
testcase_06 AC 105 ms
69,248 KB
testcase_07 AC 85 ms
55,808 KB
testcase_08 AC 43 ms
29,312 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 109 ms
70,016 KB
testcase_11 AC 75 ms
49,664 KB
testcase_12 AC 53 ms
36,352 KB
testcase_13 AC 112 ms
71,680 KB
testcase_14 AC 39 ms
26,880 KB
testcase_15 AC 61 ms
41,856 KB
testcase_16 AC 6 ms
6,944 KB
testcase_17 AC 53 ms
34,944 KB
testcase_18 AC 60 ms
39,552 KB
testcase_19 AC 7 ms
6,940 KB
testcase_20 AC 42 ms
28,544 KB
testcase_21 AC 124 ms
81,024 KB
testcase_22 AC 84 ms
54,400 KB
testcase_23 AC 129 ms
78,720 KB
testcase_24 AC 25 ms
17,536 KB
testcase_25 AC 83 ms
52,992 KB
testcase_26 AC 73 ms
47,360 KB
testcase_27 AC 73 ms
48,128 KB
testcase_28 AC 63 ms
41,856 KB
testcase_29 AC 129 ms
80,256 KB
testcase_30 AC 12 ms
9,728 KB
testcase_31 AC 51 ms
35,584 KB
testcase_32 AC 61 ms
43,392 KB
testcase_33 AC 56 ms
37,120 KB
testcase_34 AC 67 ms
44,928 KB
testcase_35 AC 69 ms
44,928 KB
testcase_36 AC 5 ms
6,940 KB
testcase_37 AC 50 ms
34,048 KB
testcase_38 AC 63 ms
42,624 KB
testcase_39 AC 60 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 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");
    }
}
0