首页 > 其他 > 详细

hdu 5562 Clarke and food(贪心)

时间:2015-11-17 16:42:04      阅读:228      评论:0      收藏:0      [点我收藏+]
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

 

hdu 5562 Clarke and food(贪心)

原文:http://www.cnblogs.com/UniqueColor/p/4972013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!