hdoj 5562 Clarke and food 【水题】

来源:互联网 发布:淘宝上二手ipad靠谱吗 编辑:程序博客网 时间:2024/05/18 13:45

Clarke and food

  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
      Total Submission(s): 654    Accepted Submission(s): 380


Problem Description
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(1T10), the number of the test cases.
For each test case: 
The first line contains two integers n,V(1n105,1V109)
The second line contains n integers, the ith integer denotes vi(1vi109).
 

Output
For each test case, print a line with an integer which denotes the answer.
 

Sample Input
13 51 3 4
 

Sample Output
2Hint: We can carry 1 and 3, the total volume of them is 5.
 



题意:给定n个物品和一个容量,问最多能够装多少个物品。


排序后,一直装到不能装为止。


AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f#define eps 1e-4#define MAXN (100000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;int a[MAXN];int main(){    int t; Ri(t);    W(t)    {        int n, V;        Ri(n); Ri(V);        for(int i = 0; i < n; i++)            Ri(a[i]);        sort(a, a+n);        int ans = 0;        for(int i = 0; i < n; i++)        {            if(V >= a[i])            {                ans++;                V -= a[i];            }            else                break;        }        Pi(ans);    }    return 0;}



0 0