hdu 5562 Clarke and food(贪心)

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(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.

 

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.
 

 

Source
 
贪心,将值从小到大排序后,一直选就可以了。
 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 100006
23 #define inf 1e12
24 int n,v;
25 int a[N];
26 int dp[N];
27 int dp_num[N];
28 int main()
29 {
30    int t;
31    scanf("%d",&t);
32    while(t--){
33       scanf("%d%d",&n,&v);
34       for(int i=0;i<n;i++){
35          scanf("%d",&a[i]);
36       }
37       sort(a,a+n);
38       int ans=0;
39       int index=0;
40       while(v>0){
41          if(v>=a[index]){
42             v-=a[index];
43             index++;
44             ans++;
45          }else{
46             break;
47          }
48 
49       }
50       printf("%d\n",ans);
51    }
52     return 0;
53 }
View Code

 

posted @ 2015-11-17 16:08  UniqueColor  阅读(213)  评论(0编辑  收藏  举报