結果

問題 No.2730 Two Types Luggage
ユーザー nautnaut
提出日時 2024-04-19 21:37:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 3,790 bytes
コンパイル時間 1,283 ms
コンパイル使用メモリ 172,108 KB
実行使用メモリ 28,520 KB
最終ジャッジ日時 2024-04-19 21:38:11
合計ジャッジ時間 7,321 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 0 ms
6,944 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 0 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 104 ms
20,860 KB
testcase_12 AC 233 ms
28,520 KB
testcase_13 AC 83 ms
18,140 KB
testcase_14 AC 103 ms
20,976 KB
testcase_15 AC 93 ms
6,940 KB
testcase_16 AC 39 ms
8,832 KB
testcase_17 AC 133 ms
25,972 KB
testcase_18 AC 120 ms
23,980 KB
testcase_19 AC 10 ms
6,944 KB
testcase_20 AC 49 ms
10,832 KB
testcase_21 AC 100 ms
19,688 KB
testcase_22 AC 135 ms
27,840 KB
testcase_23 AC 17 ms
6,940 KB
testcase_24 AC 57 ms
12,300 KB
testcase_25 AC 121 ms
21,568 KB
testcase_26 AC 27 ms
7,160 KB
testcase_27 AC 99 ms
19,692 KB
testcase_28 AC 53 ms
11,668 KB
testcase_29 AC 124 ms
24,120 KB
testcase_30 AC 96 ms
6,944 KB
testcase_31 AC 82 ms
17,912 KB
testcase_32 AC 72 ms
16,396 KB
testcase_33 AC 211 ms
27,280 KB
testcase_34 AC 214 ms
28,364 KB
testcase_35 AC 231 ms
27,376 KB
testcase_36 AC 214 ms
27,248 KB
testcase_37 AC 213 ms
27,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let M = input!(usize);
    let W = input!(usize);

    let mut A = input!(usize, N);
    let B = input!(usize, M);
    let C = input!(usize, M);

    A.sort();
    A.reverse();

    let mut BIT = BinaryIndexedTree::new(N + 1);

    for i in 0..N {
        BIT.add(i, A[i]);
    }

    let mut ans = 0;

    for s in 0..1 << M {
        let mut weight = 0;
        let mut value = 0;

        for i in 0..M {
            if (s >> i) & 1 == 1 {
                weight += B[i];
                value += C[i];
            }
        }

        if weight <= W {
            let d = W - weight;

            if d >= 1 {
                value += BIT.sum(0..std::cmp::min(d, N));
            }

            ans = std::cmp::max(ans, value);
        }
    }

    writeln!(out, "{}", ans);
}

pub struct BinaryIndexedTree<T> {
    tree: Vec<T>,
}

impl<T: Default + Clone + Copy + std::ops::AddAssign + std::ops::Sub<Output = T>>
    BinaryIndexedTree<T>
{
    /// self = [0; size]
    pub fn new(size: usize) -> Self {
        return Self {
            tree: vec![T::default(); size + 1],
        };
    }

    /// self[i] <- self[i] + w
    pub fn add(&mut self, i: usize, w: T) {
        self._inner_add(i + 1, w);
    }

    /// return Σ_{j ∈ [0, i]} self[j]
    pub fn prefix_sum(&self, i: usize) -> T {
        self._inner_sum(i + 1)
    }

    /// return Σ_{j ∈ range} self[j]
    pub fn sum<R: std::ops::RangeBounds<usize>>(&self, range: R) -> T {
        let left = match range.start_bound() {
            std::ops::Bound::Included(&l) => l,
            std::ops::Bound::Excluded(&l) => l + 1,
            std::ops::Bound::Unbounded => 0,
        };

        let right = match range.end_bound() {
            std::ops::Bound::Included(&r) => r,
            std::ops::Bound::Excluded(&r) => r - 1,
            std::ops::Bound::Unbounded => self.tree.len() - 2,
        };

        if left == 0 {
            return self.prefix_sum(right);
        } else {
            return self.prefix_sum(right) - self.prefix_sum(left - 1);
        }
    }

    fn _inner_add(&mut self, mut i: usize, w: T) {
        while i < self.tree.len() {
            self.tree[i] += w;
            i += i & i.wrapping_neg();
        }
    }

    fn _inner_sum(&self, mut i: usize) -> T {
        let mut ret = T::default();
        while i > 0 {
            ret += self.tree[i];
            i -= i & i.wrapping_neg();
        }
        return ret;
    }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0