結果

問題 No.2730 Two Types Luggage
ユーザー naut3naut3
提出日時 2024-04-19 21:37:59
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 3,790 bytes
コンパイル時間 14,114 ms
コンパイル使用メモリ 377,536 KB
実行使用メモリ 27,340 KB
最終ジャッジ日時 2024-10-11 14:23:12
合計ジャッジ時間 20,830 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 7 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 5 ms
5,248 KB
testcase_11 AC 120 ms
20,860 KB
testcase_12 AC 236 ms
27,080 KB
testcase_13 AC 253 ms
17,128 KB
testcase_14 AC 150 ms
20,812 KB
testcase_15 AC 112 ms
6,832 KB
testcase_16 AC 49 ms
8,848 KB
testcase_17 AC 203 ms
26,088 KB
testcase_18 AC 140 ms
23,980 KB
testcase_19 AC 13 ms
5,248 KB
testcase_20 AC 56 ms
10,680 KB
testcase_21 AC 117 ms
19,532 KB
testcase_22 AC 163 ms
26,740 KB
testcase_23 AC 20 ms
5,248 KB
testcase_24 AC 66 ms
12,136 KB
testcase_25 AC 124 ms
21,004 KB
testcase_26 AC 32 ms
7,164 KB
testcase_27 AC 118 ms
19,692 KB
testcase_28 AC 61 ms
11,664 KB
testcase_29 AC 145 ms
24,172 KB
testcase_30 AC 110 ms
5,248 KB
testcase_31 AC 93 ms
16,664 KB
testcase_32 AC 83 ms
15,084 KB
testcase_33 AC 249 ms
27,340 KB
testcase_34 AC 253 ms
27,212 KB
testcase_35 AC 250 ms
27,332 KB
testcase_36 AC 249 ms
27,336 KB
testcase_37 AC 248 ms
27,336 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