結果

問題 No.2869 yuusaan's Knapsacks
ユーザー maguroflymagurofly
提出日時 2024-08-30 22:57:06
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 1,649 bytes
コンパイル時間 13,400 ms
コンパイル使用メモリ 402,204 KB
実行使用メモリ 28,320 KB
最終ジャッジ日時 2024-08-30 22:57:37
合計ジャッジ時間 30,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 1 ms
6,940 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable `N` should have a snake case name
 --> src/main.rs:5:9
  |
5 |         N: usize, M: usize,
  |         ^ help: convert the identifier to snake case: `n`
  |
  = note: `#[warn(non_snake_case)]` on by default

warning: variable `M` should have a snake case name
 --> src/main.rs:5:19
  |
5 |         N: usize, M: usize,
  |                   ^ help: convert the identifier to snake case: `m`

ソースコード

diff #

use proconio::input;
const INF: i64 = 1_000_000_000_000_000_000;
fn main() {
    input! {
        N: usize, M: usize,
        e: [i64; N],
        items: [(i64, i64); M],
    }

    let knapsack_ans = e.into_iter().map(|w_max| {
        let mut dp = vec![-INF; 1 << M];
        for bits in 0 .. 1 << M {
            let mut w_sum = 0;
            let mut v_sum = 0;
            for i in 0 .. M {
                if bits & 1 << i != 0 {
                    v_sum += items[i].0;
                    w_sum += items[i].1;
                }
            }
            if w_sum <= w_max {
                dp[bits] = v_sum;
            }
        }
        dp
    }).collect::<Vec<_>>();

    let mut dp = vec![(-INF, vec![0; N]); 1 << M];
    dp[0] = (0, vec![0; N]);
    for i in 0 .. N {
        for bits in (0 .. 1 << M).rev() {
            let neg = bits ^ ((1 << M) - 1);
            let mut add = neg;
            while add > 0 {
                let v_next = dp[bits].0 + knapsack_ans[i][add];
                let mut c_next = dp[bits].1.clone();
                c_next[i] |= add;
                if dp[bits | add].0 < v_next {
                    dp[bits | add] = (v_next, c_next);
                }
                add = (add - 1) & neg;
            }
        }
    }

    let mut max = (0, vec![0; N]);
    for bits in 0 .. 1 << M {
        if max.0 < dp[bits].0 {
            max = dp[bits].clone();
        }
    }

    println!("{}", max.0);
    for i in 0 .. N {
        let items = (0 .. M).filter(|&j| max.1[i] & 1 << j != 0 ).map(|j| (j + 1).to_string() ).collect::<Vec<_>>();
        println!("{} {}", items.len(), items.join(" "));
    }
}
0