Heavy Coins

来源:互联网 发布:淘宝专卖日系足球鞋店 编辑:程序博客网 时间:2024/05/21 12:44
Gym 100712G

Heavy Coins

Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always tries to get rid of some of
the coins by using them when paying for the taxi.
Whenever Bahosain has to pay S pennies for the taxi driver, he tries to choose the maximum number of coin
pieces to pay. The driver will accept receiving more than S pennies only if he can’t remove one or more of the
given coins and still has S or more pennies.
For example, if Bahosain uses the coins of the following values: 2,7and5 to pay 11 pennies, the taxi driver
will not accept this because the coin of value 2 can be removed. On the other hand, when Bahosain uses coins
of 7 and 5to pay 11 pennies, the driver will accept it.
Note that the driver won’t give Bahosain any change back if he receives more than S pennies, and Bahosain
doesn’t care!

Input
The first line of input contains T (1 ≤ T ≤ 1001),the number of test cases.
The first line of each test case contains two integers: N (1 ≤ N ≤ 10)and S (1 ≤ S ≤ 1000),where N is the
number of coins in Bahosain’s pocket and S is the amount (in pennies) Bahosain has to pay for the taxi driver.
The next line contains N space-separated integers between 1 and 100 that represent the values (in pennies)
of the coins in Bahosain’s pocket.

Output
For each test case, print a single line with the maximum number of coins Bahosain can use to pay for the
driver.


Sample Input
2
5 9
4 1 3 5 4
7 37
7 5 8 8 5 10 4

Sample Output
3
6


Note

In the first test case, Bahosain can pay in any of the following ways: (1, 3, 5), (3, 4, 4) or (1, 4, 4).



题意:给你N个硬币,从这N个硬币中拿出若干个硬币使总和大于S,同时从你所选的这堆硬币拿走任何一个都会使总和小于S,也就是说取出若干个硬币且取出的这些硬币没有一个是多余的,求最多能拿出多少个硬币。

借鉴别人的思想DFS  这题完全没想到用DFS能解。  这题当一个模版吧。 

DFS 遍及每一个硬币的值 得出来的结果。

下面附上代码

#include <bits/stdc++.h>using namespace std;#define LL long long#define INF 1E4 * 1E9#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));const int maxn=1e3+5;const int maxx=1e6+5;/*2  T5 9  N S4 1 3 5 4       3   1 3 4 4 57 377 5 8 8 5 10 4    6*/int n,s,ans,coin[15];void dfs(int x,int y,int z){    if(y>=s)    {        ans=max(ans,z);        return ;    }    if(x<0)  return ;    dfs(x-1,y,z);// 这是遍及每一个点的精妙之处      dfs(x-1,y+coin[x],z+1);  // 这是在当前的值的情况下 加的值}int main(){    int T;    cin>>T;    while(T--)    {        ans=-INF;        cin>>n>>s;        for(int i=0;i<n;i++)            cin>>coin[i];        sort(coin,coin+n);        dfs(n-1,0,0);        cout<<ans<<endl;    }}

0 0
原创粉丝点击