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. 11636 - Hello World! (贪心法)

    Problem A Hello World!  Input: Standard Input Output: Standard Output When you first made the comput ...

  2. 【Android】Activity的菜单机制和方法解析

    Activity有一套机制来实现对菜单的管理,方法如下: 1. 初始化菜单 public boolean onCreateOptionsMenu(Menu menu) 此方法用于初始化菜单,其中men ...

  3. C++中数组初始化

    #include<iostream>using std::cout;using std::endl;int arr1[5];int arr2[5] = {1,3,5};int main() ...

  4. docker iptables 端口映射 nat

    docker  iptables  端口映射  nat #!/bin/bash pro='tcp' NAT_Host='Host_A' NAT_Port=8080 Dst_Host='Host_B' ...

  5. 自定义控件 环形进度条 ProgressBar

    使用 public class MainActivity extends Activity implements OnComompleteListener {     private int num  ...

  6. Linux命令之用户与组管理

    介绍 Linux操作系统中,任何文件都归属某一特定的用户,而任何用户都隶属至少一个用户组.用户是否有权限对某文件进行访问.读写以及执行,受到系统严格约束的正式这种清晰.严谨的用户与用户组管理系统.在很 ...

  7. js库编写的环境和准备工作

    在编写一个js库之前需要准备许多基础知识. 本人在编写之前主要参考了visjs和百度的echarts这两个库的部分内容,这之中提取出几个要点如下: 1.如何整理代码结构,使模块与模块分开又不至于出现调 ...

  8. angularJS环境安装

    第一步: 安装node.js,进入node.js官网(http://nodejs.org/)下载安装相应的node.js版本:

  9. CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用情况

    //CGRectInset 将原来的矩形放大或者缩小,正表示缩小,-表示放大. CGRect rect= CGRectMake(20, 50, 100, 80); CGRect rect1=CGRec ...

  10. javascript返回顶部几种代码总结

    纯js代码 /** * 回到页面顶部 * @param acceleration 加速度 * @param time 时间间隔 (毫秒) **/ function goTop(acceleration ...