結果

問題 No.527 ナップサック容量問題
ユーザー gyu-dongyu-don
提出日時 2017-08-22 00:34:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,266 bytes
コンパイル時間 5,260 ms
コンパイル使用メモリ 164,328 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 01:41:42
合計ジャッジ時間 2,140 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#![allow(unused_macros, unused_imports, dead_code)]

use std::io::*;
use std::collections::*;

fn read_line() -> String {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    s
}

macro_rules! from_iter {
    ($($a:ident),+ = $it:expr) => {
        $(let $a;)+
        {
            let mut _it = $it.into_iter();
            $($a = _it.next().unwrap();)+
            assert!(_it.next().is_none());
        }
    };
}

macro_rules! from_str {
    ($($a:ident : $t:ty),+ = $s:expr) => {
        $(let $a: $t;)+
        {
            let mut _it = $s.split_whitespace();
            $($a = _it.next().unwrap().parse().unwrap();)+
            assert!(_it.next().is_none());
        }
    };
}

macro_rules! from_line {
    ($($a:ident : $t:ty),+) => {
        $(let $a: $t;)+
        {
            let _line = read_line();
            let mut _it = _line.trim().split_whitespace();
            $($a = _it.next().unwrap().parse().unwrap();)+
            assert!(_it.next().is_none());
        }
    };
}

macro_rules! print_var_fmt {
    ($e:expr) => {
        concat!(stringify!($e), ":\t{}")
    };
    ($e:expr, $($es: expr),+) => {
        concat!(print_var_fmt!($e), ",\t", print_var_fmt!($($es),+))
    };
}

macro_rules! dbg {
    ($($es:expr),+) => {
        writeln!(&mut io::stderr(), print_var_fmt!($($es),*), $($es),*);
    };
}

fn main() {
    from_line!(n: u32);
    let mut arr = vec![0; 100002];
    for _ in 0..n {
        from_line!(v: u32, w: u32);
        for i in (1..(100002-w as usize)).rev() {
            if arr[i] > 0 && arr[i] + v > arr[i+w as usize] {
                arr[i+w as usize] = arr[i] + v;
            }
        }
        if arr[w as usize] <= v { arr[w as usize] = v; }
    }
    from_line!(v: u32);
    let mut min = 0;
    for (i,x) in arr.iter().enumerate() {
        if *x == v {
            min = i;
            break;
        }
    }
    let mut max = min;
    for (i, x) in arr.iter().enumerate().skip(min + 1) {
        if *x <= v {
            max = i;
        }
        else {
            break;
        }
    }
    if min == 0 {
        println!("1");
    } else {
        println!("{}", min);
    }
    if max > 100000 {
        println!("inf");
    } else {
        println!("{}", max);
    }
}
0