Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a cook, was shopping for food.
Clarke has bought food. The volume of the th food is . Now Clarke has a pack with volume . He wants to carry food as much as possible. Tell him the maxmium number he can brought with this pack.
Clarke has bought food. The volume of the th food is . Now Clarke has a pack with volume . 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
, the number of the test cases.
For each test case:
The first line contains two integers .
The second line contains integers, the th integer denotes .
For each test case:
The first line contains two integers .
The second line contains integers, the th integer denotes .
Output
For each test case, print a line with an integer which denotes the answer.
Sample Input
1 3 5 1 3 4
Sample Output
2 Hint: We can carry 1 and 3, the total volume of them is 5.
题意:输入n件食物的体积,和背包总体积,问最多能带多少件食物
分析:sort快速排序
AC代码如下:
#include "iostream"
#include<algorithm>
using namespace std;
int main(int argc, char* argv[])
{
_int64 N,i,n;
_int64 m,sum;
_int64 a[100001];
scanf("%d",&N);
while(N--)
{
sum=0;
scanf("%I64d%I64d",&n,&m);
for (i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
}
sort(a,a+n);
for(i=0;i<n;i++)
{
sum+=a[i];
if (sum>=m)
{
break;
}
}
printf("%I64d\n",i);
}
return 0;
}