【每日一题】装货物
装货物
https://ac.nowcoder.com/acm/problem/200532
题意:
思路:
#include <cstdio> #include <algorithm> using namespace std; const int N = 25; const int inf = 0x3f3f3f3f; int a[N],w[N],ans,n,x,W,mx; void dfs(int x,int cnt){ if(cnt >= ans) return; if(x > n){ ans = min(ans,cnt); return; } for(int i = 0;i < cnt;i++){ if(w[i] + a[x] <= W){ w[i] += a[x]; dfs(x + 1,cnt); w[i] -= a[x]; } } w[cnt] = a[x]; dfs(x + 1,cnt + 1); w[cnt] = 0; } int main(){ int t;scanf("%d",&t); while(t--){ mx = -inf; scanf("%d%d%d",&n,&x,&W); for(int i = 1;i <= n;i++){ scanf("%d",a + i); mx = max(mx,a[i]); } if(mx > W){puts("No");continue;} sort(a + 1,a + 1 + n,greater<int>()); ans = n; dfs(1,1); if(ans <= x) puts("Yes"); else puts("No"); } }
每日一题 文章被收录于专栏
每题一题题目