結果

問題 No.2741 Balanced Choice
ユーザー 👑 tipstar0125tipstar0125
提出日時 2024-04-20 23:55:06
言語 Rust
(1.77.0)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 3,145 ms
コンパイル使用メモリ 172,288 KB
実行使用メモリ 197,888 KB
最終ジャッジ日時 2024-04-20 23:55:13
合計ジャッジ時間 6,577 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
197,888 KB
testcase_01 AC 292 ms
197,760 KB
testcase_02 AC 296 ms
197,888 KB
testcase_03 AC 335 ms
197,888 KB
testcase_04 AC 312 ms
197,760 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 167 ms
128,384 KB
testcase_08 AC 235 ms
175,232 KB
testcase_09 AC 159 ms
121,984 KB
testcase_10 AC 209 ms
139,648 KB
testcase_11 AC 232 ms
176,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::neg_multiply)]
#![allow(dead_code)]
// use itertools::Itertools;
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, VecDeque};
// use superslice::Ext;
// use bitset_fixed::BitSet;

use proconio::{
    input,
    marker::{Chars, Usize1},
};

#[derive(Default)]
struct Solver {}
impl Solver {
    // #[fastout]
    fn solve(&mut self) {
        input! {
            N: usize,
            W: usize,
            D: isize,
            TWV: [(usize, usize, isize); N]
        }

        let mut WV0 = vec![];
        let mut WV1 = vec![];
        for &(t, w, v) in TWV.iter() {
            if t == 0 {
                WV0.push((w, v));
            } else {
                WV1.push((w, v));
            }
        }
        let L0 = WV0.len();
        let L1 = WV1.len();
        let INF = 1_isize << 60;
        let mut dp0 = vec![vec![-INF; W + 1]; L0 + 1];
        let mut dp1 = vec![vec![-INF; W + 1]; L1 + 1];
        dp0[0][0] = 0;
        dp1[0][0] = 0;

        for i in 1..=L0 {
            let (w, v) = WV0[i - 1];
            for j in 0..=W {
                dp0[i][j] = max!(dp0[i][j], dp0[i - 1][j]);
                if j + w <= W {
                    dp0[i][j + w] = max!(dp0[i][j + w], dp0[i - 1][j] + v);
                }
            }
        }
        for i in 1..=L1 {
            let (w, v) = WV1[i - 1];
            for j in 0..=W {
                dp1[i][j] = max!(dp1[i][j], dp1[i - 1][j]);
                if j + w <= W {
                    dp1[i][j + w] = max!(dp1[i][j + w], dp1[i - 1][j] + v);
                }
            }
        }
        let mut ans = 0;
        for i in 0..=W {
            for j in 0..=W {
                if (i as isize - j as isize).abs() > D || i + j > W {
                    continue;
                }
                ans = max!(ans, dp0[L0][i] + dp1[L1][j]);
            }
        }
        println!("{}", ans);
    }
}

#[macro_export]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $( $y: expr ),+) => {
        std::cmp::max($x, max!($( $y ),+))
    }
}
#[macro_export]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $( $y: expr ),+) => {
        std::cmp::min($x, min!($( $y ),+))
    }
}

fn main() {
    std::thread::Builder::new()
        .stack_size(128 * 1024 * 1024)
        .spawn(|| Solver::default().solve())
        .unwrap()
        .join()
        .unwrap();
}
0