涉及知识点:
solution:
- 题目给出的N只有18,所以我们可以想到采取爆搜的方法
- 然后题目可以稍微剪枝一下
- 1.如果花费比之前求得最小值要大,那么就不用继续向下搜了
- 2.我们可以先放体重大的再放小的,这样可以减少搜索次数
std:
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n,wight;
int ans=0x3f3f3f3f;
int w[N],car[N];
void dfs(int count,int money)
{
if(money >= ans) return ;
if(count == n+1)
{
ans = min(ans,money);
return ;
}
for (int i = 1 ; i <= money ; i ++ )
{
if(wight - car[i] >= w[count])
{
car[i] += w[count];
dfs(count+1,money);
car[i] -= w[count];
}
}
car[money+1] = w[count];
dfs(count+1,money+1);
car[count+1] = 0;
}
int main()
{
cin >> n >> wight;
for (int i = 1 ; i <= n ; i ++ ) cin >> w[i];
sort(w+1,w+n+1,greater<int>() );
dfs(0,0);
cout << ans << endl;
return 0;
}