Clarke is a patient with multiple personality disorder. One day, Clarke turned into a cook, was shopping for food.
Clarke has bought n food. The volume of the ith food is vi. Now Clarke has a pack with volume V. He wants to carry food as much as possible. Tell him the maxmium number he can brought with this pack.
Input
The first line contains an integer T(1≤T≤10), the number of the test cases.
For each test case:
The first line contains two integers n,V(1≤n≤105,1≤V≤109).
The second line contains n integers, the ith integer denotes vi(1≤vi≤109).
Output
For each test case, print a line with an integer which denotes the answer.
题意:有一个包 体积是v 后面有n种菜 每种菜有ni个单位体积 问最多能装下多少种菜
题目很简单 思路就不说了 贪心就完事 直接附代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn=1e5+5;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a[maxn];
int t,v;
scanf("%d%d",&t,&v);
for(int i=0;i<t;i++)
scanf("%d",&a[i]);
sort(a,a+t);
int s=0,sum=0;
while(s<=v)
{
s+=a[sum];
sum++;
}
printf("%d\n",sum-1);
}
}
本文介绍了一个经典的背包问题,其中一个人物Clarke患有多种人格障碍,在变为厨师人格时,面临如何用有限背包容量装下尽可能多食材的挑战。通过使用贪心算法,文章提供了一种有效的解决方案,并附上了C++实现代码。
508

被折叠的 条评论
为什么被折叠?



