結果

問題 No.527 ナップサック容量問題
ユーザー cympfhcympfh
提出日時 2017-06-20 19:26:41
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,785 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 166,704 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 06:22:20
合計ジャッジ時間 6,433 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 0 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 WA -
testcase_08 AC 0 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 WA -
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
6,940 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 3 ms
6,940 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 1 ms
6,940 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 1 ms
6,940 KB
testcase_29 WA -
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 198 ms
6,940 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 11 ms
6,944 KB
testcase_36 AC 1 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,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => ({
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    })
}
#[allow(unused_macros)]
macro_rules! swap { ($a:expr, $b:expr) => ({ let t = $b; $b = $a; $a = t; }) }

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();

    let items: Vec<_> = (0..n).map( |_| {
        let v: usize = sc.cin();
        let w: usize = sc.cin();
        (v, w)
    }).collect();

    let maxv: usize = sc.cin();

    if items.iter().fold(0, |ac, item| ac + item.0) <= maxv {
        let sumw = items.iter().fold(0, |ac, item| ac + item.1);
        println!("{}", sumw);
        println!("inf");
        return
    }

    if maxv == 0 {
        let minw = items.iter().fold(items[0].1, |ac, item| min(ac, item.1));
        println!("1");
        println!("{}", max(1, minw - 1));
        return
    }

    const M: usize = 100009;
    let inf: usize = usize::max_value() / 2;

    let mut table = vec![inf; M]; // table[sum-v] = min-w
    table[0] = 0;

    for &item in items.iter() {
        let (v, w) = item;
        for i in (0..M).rev() {
            if i < (v as usize) { continue }
            let prev = i - (v as usize);
            table[i] = min(table[i], table[prev] + w);
        }
    }

    for i in 0..M {
        if table[i] == inf { continue; }
        trace!((i, table[i]));
    }

    let minw = table[maxv as usize];
    let mut maxw = 0;
    for i in (maxv + 1)..M {
        if table[i] >= inf { continue; }
        maxw = table[i] - 1;
        break
    }

    println!("{}", minw);
    println!("{}", maxw);
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
    fn get_char(&mut self) -> char {
        self.reserve();
        let head = self.buffer[0].chars().nth(0).unwrap();
        let tail = String::from( &self.buffer[0][1..] );
        if tail.len()>0 { self.buffer[0]=tail } else { self.buffer.pop_front(); }
        head
    }
}
0