链接:
https://www.acwing.com/problem/content/167/
题意:
翰翰和达达饲养了N只小猫,这天,小猫们要去爬山。
经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<)。
翰翰和达达只好花钱让它们坐索道下山。
索道上的缆车最大承重量为W,而N只小猫的重量分别是C1、C2……CN。
当然,每辆缆车上的小猫的重量之和不能超过W。
每租用一辆缆车,翰翰和达达就要付1美元,所以他们想知道,最少需要付多少美元才能把这N只小猫都运送下山?
思路:
Dfs, 每次有坐旧车和新车.
搞一下就行, 注意排序减少可能性.
代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL Wei[20], w, a[20]; int ans = 50, n; void Dfs(int cat, int pos) { if (pos >= ans) return ; if (cat == n+1) { ans = min(ans, pos); return ; } for (int i = 1;i <= pos;i++) { if (w-Wei[i] >= a[cat]) { Wei[i] += a[cat]; Dfs(cat+1, pos); Wei[i] -= a[cat]; } } Wei[pos+1] = a[cat]; Dfs(cat+1, pos+1); Wei[pos+1] = 0; } int main() { scanf("%d%lld", &n, &w); for (int i = 1;i <= n;i++) scanf("%lld", &a[i]); sort(a+1, a+1+n, greater<LL>()); Dfs(1, 0); printf("%d\n", ans); return 0; }