Baby Coins
题意
描述
Baby 今天清点自己的百宝箱啦,箱子里有n 种硬币,硬币的面值分别是:val[1],val[2],...,val[n],每种面值的硬币都恰好有2 个。
Baby 实在闲的太无聊了,他想从他所拥有的硬币中选出若干个,使得面值之和为k。那么他的目标能否实现呢~
输入
先输入T,代表数据一共有T 组,(T ≤ 100)
每一组数据第一行都包含两个数字n(n≤18),k(1≤k≤109)
n 代表箱子中所包含的硬币种数,k 代表Baby 需要组成的金钱数额。
接下来的一行代表val[1],val[2],......,val[n]。(1≤val[i]≤ 10^7)
输出
如果Baby能组成金钱数额k,请输出Yes,否则输出No。
输入样例 1
2
2 10
3 4
3 9
1 2 10
输出样例 1
Case 1: Yes
Case 2: No
分析
每种硬币可以取 0或1或2个
所以有3^18种,暴力复杂度是很高的。所以想办法解决问题
把一半种硬币的各种组合值存在一个set里,其他在另一个set里。
最后看两个set能不能凑到k即可
set.find()的复杂度是log所以降低了整体复杂度
代码
#include<iostream>
#include<set>
using namespace std;
set<int> se1,se2;
int a[20];
void all(int up,int low,int sum,std::set<int> &se){
if(low==up){
se.insert(sum); return;
}
all(up,low+1,sum,se);
all(up,low+1,sum+a[low],se);
all(up,low+1,sum+2*a[low],se);
}
bool ok(int k){
for(set<int> ::iterator it=se1.begin();it!=se1.end();it++){
if(se2.find(k-*it)==se2.end()) continue;
return true;
}
return false;
}
int main(){
int t,n,k,kase=0;
//freopen("in.txt","r",stdin);
cin>>t;
while(t--){
cin>>n>>k;
for(int i=0;i<n;i++) cin>>a[i];
bool g=false;
if(n==1){
if(k==a[0]||k==2*a[0]) g=true;
}
else{
se1.clear(),se2.clear();
all(n/2,0,0,se1);
all(n,n/2,0,se2);
g=ok(k);
cout<<"Case "<<++kase<<": "<<(g?"Yes\n":"No\n");
}
}
return 0;
}
Baby Coins的更多相关文章
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
- Coins
Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
随机推荐
- 确认是否是因为做了物理I/O而导致的性能不佳
要获取语句是否进行了I/O,需要打开set statistics on 和set statistics on.
- 批量配置SSH互信脚本
在大规模自动化部署时我们常常需要配置好服务器的SSH互信,以便自动化脚本可以免密登录远程服务器,常规的手动配置SSH互信步骤如下: 使用ssh-keygen生成本地ssh key(mha01),生成的 ...
- JSON Web Tokens简单学习
JWT用于加密生成安全认证的令牌,存储登录验证成功的部分用户信息 一.安装JWT 二.加密 解密 代码 /*存储在加密字符串的信息*/ var payload = new Dictionary< ...
- March 01st, 2018 Week 9th Thursday
Let bygones be bygones. 过去的就让它过去吧. What happened has happened, it cannot be undone, so just leave it ...
- February 27th, 2018 Week 9th Tuesday
Great minds think alike. 英雄所见略同. If great minds really did think alike, then we would live in an unr ...
- 微服务---Eureka注册中心(服务治理)
在上一篇的初识SpringCloud微服务中,我们简单讲解到服务的提供者与消费者,当服务多了之后,会存在依赖与管理之间混乱的问题,以及需要对外暴露自己的地址,为了解决此等问题,我们学习Eureka注册 ...
- TIPS FOR IMPROVING PERFORMANCE OF KAFKA PRODUCER
When we are talking about performance of Kafka Producer, we are really talking about two different t ...
- 解决Go依赖包安装问题
最近在学习Go编写后端服务,先放出谢大的书镇楼: https://github.com/astaxie/build-web-application-with-golang/blob/master/zh ...
- Git无法提交,报错
Failed at the ant-design-pro@2.2.1 lint-staged script. npm ERR! This is probably not a problem with ...
- java语言的特征
运行时:反射与内省+派发机制: 额外的多态支持:注解: 语法改进:内部类与匿名类.匿名函数: 线程支持改进: 类加载机制? aop的支持: bean?