ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举
- 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, 7 and 5 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 5 to 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 |
Sample Output |
|||||
2 |
3 |
|||||
5 |
9 |
6 |
||||
4 |
1 3 |
5 |
4 |
|||
7 |
37 |
|||||
7 |
5 8 |
8 |
5 |
10 |
4 |
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).
/*
题意:
A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士;
的士司机可以不找零,但是的士司机也不会多收零钱。
怎么样才能使 A 花的零钱最多。 思路-暴力
2^10*1000=1024000 102W 完全不会超时。
直接暴力枚举 方法:
枚举子集,用二进制思想来枚举子集。 */ #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define memset(a,b) memset(a,b,sizeof(a))
#define MX 10000 + 50
using namespace std;
int maxx,n,m,a[MX]; bool cmp(int x,int y) {
return x>y;
} void dfs(int x,int sum,int num) {
if(sum>=m) {
maxx=max(maxx,num); //如果花掉的硬币值已经够坐车了,更新最大花掉的硬币数
return ;
}
if(x==n)return; //如果已经查询了所有的硬币返回。
dfs(x+1,sum+a[x],num+1); //递归下去 搜索取下一个硬币的情况
dfs(x+1,sum,num); //搜索不取下一个硬币的情况
} int main() {
int T,num;
cin>>T;
while(T--) {
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
maxx=0;
sort(a,a+n,cmp); //按照硬币的枝排序;
dfs(0,0,0);
printf("%d\n",maxx);
}
return 0;
}
ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举的更多相关文章
- ACM: 限时训练题解-Runtime Error-二分查找
Runtime Error Bahosain was trying to solve this simple problem, but he got a Runtime Error on one ...
- ACM: 限时训练题解-Rock-Paper-Scissors-前缀和
Rock-Paper-Scissors Rock-Paper-Scissors is a two-player game, where each player chooses one of Roc ...
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- ACM: 限时训练题解-Epic Professor-水题
Epic Professor Dr. Bahosain works as a professor of Computer Science at HU (Hadramout Universit ...
- ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】
Street Lamps Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...
- 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵 基准时间限制:1 秒 空 ...
- Gym - 100712G Heavy Coins(二进制枚举)
https://vjudge.net/problem/Gym-100712G 题意:给出n枚不同价值的硬币和一个总价S,现在要选择尽量多的硬币来大于等于S,要求是比如说现在选择的硬币的总和为sum,那 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
随机推荐
- CLR via C#(05)- 访问限定、数据成员
今天跟大家分享一下关于访问限定和数据成员的知识.主要包括以下两点: Abstract, sealed, virtual, new, override怎么用? Const 和 readonly好像都表示 ...
- Redis笔记(八)Redis的持久化
Redis相比Memcached的很大一个优势是支持数据的持久化, 通常持久化的场景一个是做数据库使用,另一个是Redis在做缓存服务器时,防止缓存失效. Redis的持久化主要有快照Snapshot ...
- Overview and Evaluation of Bluetooth Low Energy: An Emerging Low-Power Wireless Technology
转自:http://www.mdpi.com/1424-8220/12/9/11734/htm Sensors 2012, 12(9), 11734-11753; doi:10.3390/s12091 ...
- 让那些为Webkit优化的网站也能适配IE10(转载)
转载地址:http://www.w3cplus.com/css3/adapting-your-webkit-optimized-site-for-internet-explorer-10.html 特 ...
- SQL Server 2014 BI新特性(一)五个关键点带你了解Excel下的Data Explorer
Data Explorer是即将发布的SQL Server 2014里的一个新特性,借助这个特性讲使企业中的自助式的商业智能变得更加的灵活,从而也降低了商业智能的门槛. 此文是在微软商业智能官方博客里 ...
- 攻城狮在路上(叁)Linux(二十一)--- linux磁盘检查 fsck \ badblocks
若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查.这一步是可选的,尽量少用. 使用前的建议:使用fsck命令时,被检查的分区务必不要挂载在系统上. 一.fsck: 命令格式:fsck [ ...
- ASP.NET 5探险(8):利用中间件、TagHelper来在MVC 6中实现Captcha
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于ASP.NET 5及MVC 6是一个微软全新重新的Web开发平台,之前一些现有的验 ...
- MySQL5.5出面ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)问题的解决办法
问题描述 安装完MySQL5.5数据库,使用Navicat Premium以及命令窗口连接数据库都报以下错误: ERROR 1045 (28000): Access denied for user ' ...
- Lucene.net 全文检索 盘古分词
lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...
- 在Salesforce中处理Email的发送
在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送 请看如下一段简单代码: public boolean TextFormat {get;se ...