1 solutions

  • 0
    @ 2024-8-28 22:48:14
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 25;
    
    int a[N], cab[N];
    int n, w, ans;
    
    void dfs(int cnt, int now) {
        if(cnt >= ans) return;
        if(now == n) {
            ans = cnt;
            return;
        }
        for(int i = 1; i <= cnt; i ++ ) {
            if(cab[i] + a[now] <= w) {
                cab[i] += a[now];
                dfs(cnt, now + 1);
                cab[i] -= a[now];
            }
        }
        cab[cnt + 1] += a[now];
        dfs(cnt + 1, now + 1);
        cab[cnt + 1] -= a[now];
    }
    
    int main() {
        cin >> n >> w;
        for(int i = 0; i < n; i ++ ) cin >> a[i];
        sort(a, a + n, greater{});
        ans = n;
        dfs(1, 0);
        cout << ans << endl;
        return 0;
    }
    
    • @ 2024-8-28 22:49:03

      第一个剪枝: 优化搜索顺序 第二个剪枝: 最优性剪枝

  • 1

Information

ID
481
Time
1000ms
Memory
256MiB
Difficulty
10
Tags
# Submissions
3
Accepted
2
Uploaded By