結果

問題 No.2869 yuusaan's Knapsacks
ユーザー maguroflymagurofly
提出日時 2024-08-30 22:58:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 3,438 ms / 4,500 ms
コード長 1,737 bytes
コンパイル時間 15,592 ms
コンパイル使用メモリ 400,568 KB
実行使用メモリ 21,376 KB
最終ジャッジ日時 2024-08-30 22:59:01
合計ジャッジ時間 39,895 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 519 ms
9,600 KB
testcase_04 AC 512 ms
9,728 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1,146 ms
21,376 KB
testcase_07 AC 661 ms
9,600 KB
testcase_08 AC 201 ms
6,944 KB
testcase_09 AC 79 ms
6,944 KB
testcase_10 AC 710 ms
13,184 KB
testcase_11 AC 444 ms
7,680 KB
testcase_12 AC 1,307 ms
17,792 KB
testcase_13 AC 396 ms
7,552 KB
testcase_14 AC 968 ms
11,136 KB
testcase_15 AC 528 ms
11,136 KB
testcase_16 AC 698 ms
11,648 KB
testcase_17 AC 2,382 ms
21,376 KB
testcase_18 AC 1,504 ms
17,280 KB
testcase_19 AC 1,139 ms
13,696 KB
testcase_20 AC 94 ms
6,940 KB
testcase_21 AC 1,371 ms
15,360 KB
testcase_22 AC 2,321 ms
15,232 KB
testcase_23 AC 303 ms
9,088 KB
testcase_24 AC 1,958 ms
17,792 KB
testcase_25 AC 285 ms
7,040 KB
testcase_26 AC 3,438 ms
15,232 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1,018 ms
21,376 KB
testcase_29 AC 1,158 ms
21,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 {
                if knapsack_ans[i][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