跳到正文

题解

题解:AT_abc479_c [ABC479C] Bento Packing

srx121215约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月13日
收录
2026年11月26日
更新
2026年11月26日
篇幅
约 2 分钟

版本

  1. v1当前

一个比较简单的题。

题目大意

有n种配菜,每种有一个美味程度 aia_i 和体积 bib_i 你要选择一些配菜,使所选配菜的总体积不超过 W,并且美味程度尽可能大。

思路

看到这题的第一眼这不是 0/1 背包板子吗?然后看数据范围 W<=109W <= 10^9,但是 n<=20n <= 20 所以这题dfs爆搜就可以过。

时间复杂度 O(2n)O(2^n)。

代码

C++
#include<bits/stdc++.h>
#define push_back emplace_back
#define int long long
#define debug cout << "!" << '\n';
#define QwQ 0
using namespace std;
int n, w;
int a[30], b[30];
int ans = 0;
void dfs(int x, int cnt, int sum){ \\ x表示选到第x的位置, cnt表示目前所选的配菜的总体积, sum表示所选的配菜的美味度之和
    if(cnt > w) return;
    if(x > n){
        ans = max(ans, sum);
        return;
    }
    dfs(x + 1, cnt + b[x], sum + a[x]); \\选
    dfs(x + 1, cnt, sum); \\不选
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> w;
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i];
    }
    dfs(1, 0, 0);
    cout << ans << '\n';
    return QwQ;
}

完结撒花!!!

原文