Problem Description

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4
解题思路:这题用二进制解法在poj却超时了,而杭电却不会(单调队列就别想了-->TLE);考虑多重部分和问题,时间复杂度是O(nW)。
hdu 2844 #AC代码(343ms):有坑,测试数据种W有为负数的,这就是一直RE的原因=_=...简单判断一下即可。
 #include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
if(W<=){puts("");continue;}
memset(dp,-,sizeof(dp));dp[]=;//注意初始化dp[0]为0
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;//面额太大或者或者在配更小的数时数量已经用光了
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));//统计不小于0的个数
printf("%d\n",ans);
}
return ;
}

poj 1742 #AC代码(1813ms):中规中矩,W不会出现负数。

 #include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
memset(dp,-,sizeof(dp));dp[]=;
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));
printf("%d\n",ans);
}
return ;
}

题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)的更多相关文章

  1. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  2. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  3. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  4. POJ 1742 Coins (多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Descriptio ...

  5. poj 1742 Coins(dp之多重背包+多次优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  6. [POJ 1742] Coins 【DP】

    题目链接:POJ - 1742 题目大意 现有 n 种不同的硬币,每种的面值为 Vi ,数量为 Ni ,问使用这些硬币共能凑出 [1,m] 范围内的多少种面值. 题目分析 使用一种 O(nm) 的 D ...

  7. poj 1742 Coins (多重背包)

    http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...

  8. Poj 1742 Coins(多重背包)

    一.Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dolla ...

  9. poj 1742 Coins(二进制拆分+bitset优化多重背包)

    \(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...

随机推荐

  1. CEF3研究(二)

    应用程序结构 每个CEF3应用程序都有一个相同的结构: 提供一个入口函数以初始化CEF和运行每个子进程逻辑和CEF消息处理 提供一个CefApp子类处理某个进程的回调 提供一个CefClinet子类处 ...

  2. 一例Ext4文件系统fsck后损坏的修复过程

    1.故障发生背景 Ext4文件系统没有umount下来,之后做了fsck操作检查一致性,结果导致Ext4文件mount不上(有时也会表现为导致目录变成了文件). 报错提示信息:mount: wrong ...

  3. Antivius for Linux

    http://www.clamav.net/   https://www.avast.com/zh-cn/linux-server-antivirus   http://www.f-prot.com/ ...

  4. OpenCV实践之路——Python的安装和使用

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/50936076 微博:http ...

  5. Android5.0(lollipop)新特性介绍(一)

    今年6月的Google I/O大会上.Android L的初次见面我相信让会让非常多android粉丝有些小激动和小期待.当然作为开发人员的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本 ...

  6. 实战c++中的string系列--指定浮点数有效数字并转为string

    上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...

  7. 理解Paxos Made Practical

    Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...

  8. Redis管理key命令

    1 DEL key该命令用于在 key 存在时删除 key. 2 DUMP key 序列化给定 key ,并返回被序列化的值. 3 EXISTS key 检查给定 key 是否存在. 4 EXPIRE ...

  9. net start sshd 发生系统错误1069--cygwin安装过程

    net start sshd 发生系统错误1069 解决方法: services.msc调出服务,然后CYGWIN sshd服务->属性,修改账户的名字和密码(win7的登录名和密码) 可能还遇 ...

  10. 谈谈对redux的认识

    redux是从flux演变而来,但又独立于react.简言之就是,redux是一种单纯的状态管理器.可以和react搭配,也可以和其它框架搭配. redux有三个重要的部分组成: store, act ...