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
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
- Heavy Coins
- Heavy Coins codeforces
- Gym-100712G-Heavy Coins
- Gym 100712G Heavy Coins
- Gym 100712G Heavy Coins(DFS)
- G. Heavy Coins(二进制枚举)
- GYM 100712 G.Heavy Coins(枚举)
- Gym 100712G Heavy Coins(二进制枚举子集)
- Coins
- Coins
- Coins
- Coins
- coins
- Coins
- Coins
- Coins
- Coins
- Heavy Traffic
- Android 单例设计模式书写
- Gulp压缩、合并js/css文件,压缩图片以及热更新教程
- dom知识点整
- 分布式之ZooKeeper
- [翻译自mos文章]Oracle多租户选项-12c,FAQ
- Heavy Coins
- 数据结构与算法分析笔记与总结(java实现)--字符串6:两串旋转练习题(*)
- NSMutableArray使用addObject方法无效
- 开源点评:ZeroMQ简介
- php中require(),include(),require_once()和include_once()区别
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 详细的log4j配置使用流程
- 转:MySQL 5.6内存占用过高解决方案
- Unreal 4引擎中,使用BehaviorTree控制AI行为的C++实现