結果

問題 No.527 ナップサック容量問題
ユーザー SeiyaSeiya
提出日時 2017-07-23 21:37:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 837 ms
コンパイル使用メモリ 165,756 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 16:57:06
合計ジャッジ時間 2,170 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 13 ms
5,376 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 13 ms
5,376 KB
testcase_11 AC 9 ms
5,376 KB
testcase_12 AC 7 ms
5,376 KB
testcase_13 AC 13 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
testcase_18 AC 7 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 14 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 8 ms
5,376 KB
testcase_27 AC 9 ms
5,376 KB
testcase_28 AC 7 ms
5,376 KB
testcase_29 AC 13 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 7 ms
5,376 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 9 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 7 ms
5,376 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::stdin;
use std::cmp::max;

struct Object {
    v: usize,
    w: usize
}

fn main(){
    let n = getvec()[0];
    let vws = (|| {
        let mut vws: Vec<_> = Vec::new();
        for _ in 0..n {
            let vw = getvec();
            vws.push(Object{v: vw[0], w: vw[1]});
        }
        vws
    })();
    let v = getvec()[0];

    let (w_min, w_max) = solve(&vws, v);
    println!("{}", if v==0 {1} else {w_min});
    if w_max==-1 {
        println!("inf");
    } else {
        println!("{}", w_max);
    }
}

fn solve(vws: &Vec<Object>, v_max: usize) -> (i64, i64) {
    let mut dp: Vec<usize> = Vec::new();
    for _ in  0..100_000 { dp.push(0) }

    for &Object{v, w} in vws {
        for i in (0..(100_000 as i64 - w as i64) as usize).rev() {
            dp[i+w] = max(dp[i+w], dp[i]+v);
        }
    }

    let mut w_min = -1;
    let mut w_max = -1;
    for (i, &v) in dp.iter().enumerate() {
        if w_min==-1 && v==v_max {
            w_min = i as i64;
        } else if w_min !=-1 && v > v_max {
            w_max = i as i64 -1;
            break;
        }
    }

    (w_min, w_max)
}

#[allow(dead_code)]
fn getline() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn getvec<T: std::str::FromStr>() -> Vec<T>{
    let line = getline();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn getpair() -> (usize, usize){
    let nm = getvec();
    (nm[0], nm[1])
}
0