結果

問題 No.2492 Knapsack Problem?
ユーザー colognecologne
提出日時 2023-10-30 15:34:04
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 5,024 bytes
コンパイル時間 12,752 ms
コンパイル使用メモリ 378,396 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-25 17:17:11
合計ジャッジ時間 12,375 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `0` is never read
  --> src/main.rs:97:13
   |
97 | struct Line(String);
   |        ---- ^^^^^^
   |        |
   |        field in this struct
   |
   = note: `#[warn(dead_code)]` on by default
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
   |
97 | struct Line(());
   |             ~~

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(unused_imports)]
use std::mem::*;

fn solve(mut io: Reader) {
    let n = io.read();
    let ww: i32 = io.read();
    let mut vw = vec![(0, 0); n];
    for (v, w) in &mut vw {
        *v = io.read();
        *w = io.read();
    }
    println!(
        "{}",
        vw.into_iter()
            .filter(|&(_, w)| w <= ww)
            .map(|x| x.0)
            .max()
            .unwrap_or(-1)
    );
}

// Reader
fn main() {
    let reader = Reader::new();
    solve(reader);
    STDOUT.with(|cell| cell.borrow_mut().flush().ok());
}

struct Reader<'a> {
    buf: Vec<u8>,
    off: usize,
    len: usize,
    stdin: StdinLock<'a>,
}

const BUFFER_SIZE: usize = 1 << 17;
impl Reader<'_> {
    fn new() -> Self {
        Self {
            buf: vec![0; BUFFER_SIZE],
            off: BUFFER_SIZE,
            len: BUFFER_SIZE,
            stdin: stdin().lock(),
        }
    }
    fn skip_whitespace(&mut self) {
        while self.len > 0 {
            if let Some(i) = self.buf[self.off..self.len].iter().position(|&b| b > b' ') {
                self.off += i;
                break;
            }
            self.fill();
        }
    }
    fn remain(&self) -> &[u8] {
        &self.buf[self.off..self.len]
    }
    #[cold]
    fn fill(&mut self) {
        let len = self.stdin.read(&mut self.buf).unwrap();
        self.len = len;
        self.off = 0;
    }
    fn read<T: Readable>(&mut self) -> T {
        self.skip_whitespace();
        T::from_reader(self)
    }
}

trait Readable {
    fn from_reader(r: &mut Reader) -> Self;
}

impl Readable for String {
    fn from_reader(r: &mut Reader) -> Self {
        let mut s = Self::new();
        while r.len > 0 {
            let remain = r.remain();
            if let Some(i) = remain.iter().position(|&b| b <= b' ') {
                unsafe { s.as_mut_vec() }.extend_from_slice(&remain[..i]);
                r.off += i;
                break;
            } else {
                unsafe { s.as_mut_vec() }.extend_from_slice(remain);
            }
            r.fill();
        }
        s
    }
}

struct Line(String);

impl Readable for Line {
    fn from_reader(r: &mut Reader) -> Self {
        let mut s = String::new();
        while r.len > 0 {
            let remain = r.remain();
            if let Some(i) = remain.iter().position(|&b| b < b' ') {
                unsafe { s.as_mut_vec() }.extend_from_slice(&remain[..i]);
                r.off += i;
                break;
            } else {
                unsafe { s.as_mut_vec() }.extend_from_slice(remain);
            }
            r.fill();
        }
        Line(s)
    }
}

macro_rules! readable_signed {
    ($($t:ty)+) => {
        $(
        impl Readable for $t {
            fn from_reader(r: &mut Reader) -> Self {
                let mut neg = None;
                let mut x = 0u64;
                'next_char: while r.len > 0 {
                    for (i, &b) in r.remain().iter().enumerate() {
                        if b == b'-' && neg.is_none() {
                            neg = Some(true);
                        } else if b.is_ascii_digit() {
                            x = x * 10 + b as u64 - 48;
                        } else {
                            r.off += i;
                            break 'next_char;
                        }
                    }
                    r.fill();
                }
                if neg == Some(true) {
                    x.wrapping_neg() as $t
                } else {
                    x as $t
                }
            }
        }
        )+
    };
}
macro_rules! readable_unsigned {
    ($($t:ty)+) => {
        $(
        impl Readable for $t {
            fn from_reader(r: &mut Reader) -> Self {
                let mut x = 0;
                'next_char: while r.len > 0 {
                    for (i, &b) in r.remain().iter().enumerate() {
                        if b.is_ascii_digit() {
                            x = x * 10 + b as $t - 48;
                        } else {
                            r.off += i;
                            break 'next_char;
                        }
                    }
                    r.fill();
                }
                x
            }
        }
        )+
    };
}

readable_signed!(i8 i16 i32 i64 i128 isize);
readable_unsigned!(u8 u16 u32 u64 u128 usize);

thread_local! {
    static STDOUT: std::cell::RefCell<BufWriter<StdoutLock<'static>>> = std::cell::RefCell::new(std::io::BufWriter::with_capacity(1 << 17, stdout().lock()));
}

#[macro_export]
macro_rules! println {
    ($($t:tt)*) => {
        STDOUT.with(|cell| writeln!(cell.borrow_mut(), $($t)*).unwrap());
    };
}
#[macro_export]
macro_rules! print {
    ($($t:tt)*) => {
       STDOUT.with(|cell| write!(cell.borrow_mut(), $($t)*).unwrap());
    };
}
#[macro_export]
macro_rules! flush {
    () => {
        STDOUT.with(|cell| cell.borrow_mut().flush().unwrap());
    };
}
0