Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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(<=n<=),m(m<=).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (<=Ai<=,<=Ci<=). The last test case is followed by two zeros.

Output

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

Sample Input


Sample Output


Source

 

传说中的男人八题,是男人就A这八题。有n种面额的硬币,面额个数分别为A_i、C_i,求最多能搭配出几种不超过m的金额?

这是一个多重部分和问题(多重背包问题),放在了《2.3 记录结果再利用的“动态规划” 优化递推关系式》。最基本的做法是:

dp[i][j] := 用前i种硬币能否凑成j

递推关系式:

dp[i][j] = (存在k使得dp[i – 1][j – k * A[i]]为真,0 <= k <= m 且下标合法)

然后三重循环ijk递推

 #include <iostream>
#include <algorithm>
using namespace std; bool dp[ + ][ + ]; // dp[i][j] := 用前i种硬币能否凑成j
int A[ + ];
int C[ + ]; ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, m;
while(cin >> n >> m && n > )
{
memset(dp, , sizeof(dp));
for (int i = ; i < n; ++i)
{
cin >> A[i];
}
for (int i = ; i < n; ++i)
{
cin >> C[i];
}
dp[][] = true;
for (int i = ; i < n; ++i)
{
for (int j = ; j <= m; ++j)
{
for (int k = ; k <= C[i] && k * A[i] <= j; ++k)
{
dp[i + ][j] |= dp[i][j - k * A[i]];
}
}
}
int answer = count(dp[n] + , dp[n] + + m , true); // 总额0不算在答案内
cout << answer << endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
///////////////////////////End Sub//////////////////////////////////

这种代码不用提交也知道会TLE,因为这个朴素的算法的复杂度是O(m∑iCi),比如那第二个用例画成图的话会看到:

解释一下,dp数组和更新顺序为:

第二个用例:

————–
因为可以用0个1加上dp[][]拼成0;所以,dp[][]被更新为真
因为可以用1个1加上dp[][]拼成1;所以,dp[][]被更新为真
因为可以用2个1加上dp[][]拼成2;所以,dp[][]被更新为真 ————–
因为可以用0个4加上dp[][]拼成0;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成1;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成2;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成4;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成5;所以,dp[][]被更新为真 ————– 顺便第一个用例: ————–
因为可以用0个1加上dp[][]拼成0;所以,dp[][]被更新为真
因为可以用1个1加上dp[][]拼成1;所以,dp[][]被更新为真
因为可以用2个1加上dp[][]拼成2;所以,dp[][]被更新为真 ————–
因为可以用0个2加上dp[][]拼成0;所以,dp[][]被更新为真
因为可以用0个2加上dp[][]拼成1;所以,dp[][]被更新为真
因为可以用0个2加上dp[][]拼成2;所以,dp[][]被更新为真
因为可以用1个2加上dp[][]拼成3;所以,dp[][]被更新为真
因为可以用1个2加上dp[][]拼成4;所以,dp[][]被更新为真 ————–
因为可以用0个4加上dp[][]拼成0;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成1;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成2;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成3;所以,dp[][]被更新为真
因为可以用0个4加上dp[][]拼成4;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成5;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成6;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成7;所以,dp[][]被更新为真
因为可以用1个4加上dp[][]拼成8;所以,dp[][]被更新为真 ————–

这个算法每次只记录一个bool值,损失了不少信息。在这个问题中,不光能够求出是否能得到某个金额,同时还能把得出了此金额时A_i还剩下多少个算出来,这样直接省掉了k那重循环。

优化dp定义:

dp[i][j] := 用前i种硬币凑成j时第i种硬币最多能剩余多少个(-1表示配不出来)
如果dp[i - ][j] >= (前i-1个数可以凑出j,那么第i个数根本用不着)直接为C[i]
dp[i][j] = 如果j < A[i]或者dp[i][j - a[i]] <= (面额太大或者在配更小的数的时候就用光了)-
其他(将第i个数用掉一个) dp[i][j-a[i]] -

最后统计一下dp数组第n行>=0的个数就知道答案了:

 #include <iostream>
#include <algorithm>
using namespace std; int dp[ + ][ + ]; // dp[i][j] := 用前i种硬币凑成j时第i种硬币最多能剩余多少个
int A[ + ];
int C[ + ]; ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, m;
while(cin >> n >> m && n > )
{
memset(dp, -, sizeof(dp));
dp[][] = ;
for (int i = ; i < n; ++i)
{
cin >> A[i];
}
for (int i = ; i < n; ++i)
{
cin >> C[i];
}
for (int i = ; i < n; ++i)
{
for (int j = ; j <= m; ++j)
{
if (dp[i][j] >= )
{
dp[i + ][j] = C[i];
}
else if (j < A[i] // 用一个就超出,不能用
|| dp[i + ][j - A[i]] <= ) // 连凑比j小的数的时候都用完了,此时更加用完了
{
dp[i + ][j] = -;
}
else
{
dp[i + ][j] = dp[i + ][j - A[i]] - ; // 用上了一个第i个硬币
}
}
}
int answer = count_if(dp[n] + , dp[n] + + m , bind2nd(greater_equal<int>(), )); // 总额0不算在答案内
cout << answer << endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
///////////////////////////End Sub//////////////////////////////////

还是拿第二个用例画个图:

第二个用例:
- - - - -
- - - - - -
- - - - - -
————–
dp[][]不为负,dp[][]更新为硬币0的个数。
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
- - - - -
- - -
- - - - - -
————–
dp[][]不为负,dp[][]更新为硬币1的个数。
dp[][]不为负,dp[][]更新为硬币1的个数。
dp[][]不为负,dp[][]更新为硬币1的个数。
余额太大,dp[][]更新为硬币1的个数。
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
- - - - -
- - -
-
————– 第一个用例:
- - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
————–
dp[][]不为负,dp[][]更新为硬币0的个数。
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
dp[][]用完了,dp[][]更新为硬币0的个数。
- - - - - - - - - -
- - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
————–
dp[][]不为负,dp[][]更新为硬币1的个数。
dp[][]不为负,dp[][]更新为硬币1的个数。
dp[][]不为负,dp[][]更新为硬币1的个数。
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]用完了,dp[][]更新为硬币1的个数。
dp[][]用完了,dp[][]更新为硬币1的个数。
dp[][]用完了,dp[][]更新为硬币1的个数。
dp[][]用完了,dp[][]更新为硬币1的个数。
dp[][]用完了,dp[][]更新为硬币1的个数。
dp[][]用完了,dp[][]更新为硬币1的个数。
- - - - - - - - - -
- - - - - - - -
- - - - - -
- - - - - - - - - - -
————–
dp[][]不为负,dp[][]更新为硬币2的个数。
dp[][]不为负,dp[][]更新为硬币2的个数。
dp[][]不为负,dp[][]更新为硬币2的个数。
dp[][]不为负,dp[][]更新为硬币2的个数。
dp[][]不为负,dp[][]更新为硬币2的个数。
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]其他,dp[][]更新为dp[][]
dp[][]用完了,dp[][]更新为硬币2的个数。
dp[][]用完了,dp[][]更新为硬币2的个数。
- - - - - - - - - -
- - - - - - - -
- - - - - -
- -
————–

本以为这次照着书上的思路来的应该没问题了吧,数组再利用就懒得做了。于是提交,结果MLE

于是打起精神来重复利用数组,注意到上图中的箭头都是垂直的,也就是说可以定义

dp[j] := 在第i次循环时之前表示用前i-1种硬币凑成j时第i种硬币最多能剩余多少个(-1表示配不出来),循环之后就表示第i次的状态

于是就省了一维数组:

 #include <iostream>
#include <set>
#include <algorithm>
using namespace std; int dp[ + ]; // dp[i][j] := 用前i种硬币凑成j时第i种硬币最多能剩余多少个
int A[ + ];
int C[ + ]; ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n, m;
while(cin >> n >> m && n > )
{
memset(dp, -, sizeof(dp));
dp[] = ;
for (int i = ; i < n; ++i)
{
cin >> A[i];
}
for (int i = ; i < n; ++i)
{
cin >> C[i];
}
for (int i = ; i < n; ++i)
{
for (int j = ; j <= m; ++j)
{
if (dp[j] >= )
{
dp[j] = C[i];
}
else if (j < A[i] // 用一个就超出,不能用
|| dp[j - A[i]] <= ) // 连凑比j小的数的时候都用完了,此时更加用完了
{
dp[j] = -;
}
else
{
dp[j] = dp[j - A[i]] - ; // 用上了一个第i个硬币
}
}
}
int answer = count_if(dp + , dp + + m , bind2nd(greater_equal<int>(), )); // 总额0不算在答案内
cout << answer << endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
///////////////////////////End Sub//////////////////////////////////

提交,AC,并且足足算了2秒钟。这就是男人八题么

 

poj 1742 Coins(dp之多重背包+多次优化)的更多相关文章

  1. poj 1742 Coins(二进制优化多重背包)

    传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...

  2. POJ 1742 Coins DP 01背包

    dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态. a.当dp[i-1][j]>=0时,dp[i][j]=ci; b.当j-ai>=0& ...

  3. hdu 2844 poj 1742 Coins

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

  4. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  5. POJ 1742 Coins ( 经典多重部分和问题 && DP || 多重背包 )

    题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...

  6. POJ 1742 Coins 【多重背包DP】

    题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...

  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 dollar. ...

  9. Poj 1742 Coins(多重背包)

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

随机推荐

  1. 使用java连接AD域,验证账号password是否正确

    web项目中有时候客户要求我们使用ad域进行身份确认,不再另外做一套用户管理系统.事实上客户就是仅仅要一套账号能够訪问全部的OA.CRM等办公系统. 这就是第三方验证.一般有AD域,Ldap,Radi ...

  2. Swift开放StatsD后上传数据的出现,出现退换货503的Bug

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/40781569 swift在版本号2.1.0之前假设各个服务的配置文件里打开下面配置后,且系统 ...

  3. HDU 2159 FATE(全然背包+二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. MVC实现类似QQ的网页聊天功能-Ajax(上)

    说到QQ聊天,程序员首先想到的就是如何实现长连接,及时的接收并回馈信息.那么首先想到的就是Ajax,Ajax的运行机制是通过XMLHttpRequest向服务器发出异步请求,并接受数据,这样就可以实现 ...

  5. openOffice安装

    [root@rusky openOffice]# tar -zxvf OOo_3..0_Linux_x86_install-rpm-wJRE_zh-CN.tar.gz [root@rusky open ...

  6. Dialog( 对话框) 组件

    一. 加载方式//class 加载方式<div class="easyui-dialog" title="My Dialog"style="wi ...

  7. 查看 SELinux状态及关闭SELinux

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  8. ORACLE基本SQL语句-用户及建表篇

    一.用户相关SQL语句 /*新建用户*/create user ; 说明:SA用户名,2013密码 /*授权connect,resource给用户sa*/grant connect,resource ...

  9. tomcat下出现The file is absent or does not have execute&

    启动tomcat出现The file is absent or does not have execute permission... Cannot find bin/catalina.sh The ...

  10. hdu5353

    模拟,,, 每个人有一些糖果,每两个人之间只能给一个糖果,问最后是否能让所有人的糖果数量相同,只要确定一个糖果的流向其他的就能够确定. 马虎了,卡了好几天,心塞塞的... #include<io ...