AcWing 165. 小猫爬山
原题链接
简单
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 20;
int n, w, res = N;
int weight[N], sum[N];
void dfs(int cat, int car){//枚举到了第几只小猫,第几辆卡车
if(car >= res) return;
if(cat == n){
res = car;
return;
}
for(int i = 0; i < car; i++){
if(sum[i] + weight[cat] <= w){
sum[i] += weight[cat];
dfs(cat + 1, car);
sum[i] -= weight[cat];
}
}
sum[car] = weight[cat];//新开一辆车
dfs(cat + 1, car + 1);
sum[car] = 0;
}
int main(){
cin >> n >> w;
for(int i = 0; i < n; i++) cin >> weight[i];
sort(weight, weight + n);
reverse(weight, weight + n);
dfs(0, 0);
cout << res << endl;
return 0;
}