dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态。

a.当dp[i-1][j]>=0时,dp[i][j]=ci;

b.当j-ai>=0&&dp[i-1][j-ai]>0时,dp[i][j]=dp[i-1][j-ai]-1;

c.其他,dp[i][j]=-1

  1. Source Code
  2.  
  3. Problem: User: BMan
  4. Memory: 1112K Time: 1547MS
  5. Language: G++ Result: Accepted
  6. Source Code
  7. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<cstdlib>
  11. #include<algorithm>
  12. #include<iostream>
  13. #include<sstream>
  14. #include<cmath>
  15. #include<climits>
  16. #include<string>
  17. #include<map>
  18. #include<queue>
  19. #include<vector>
  20. #include<stack>
  21. #include<set>
  22. using namespace std;
  23. typedef long long ll;
  24. typedef unsigned long long ull;
  25. typedef pair<int,int> pii;
  26. #define pb(a) push(a)
  27. #define INF 0x1f1f1f1f
  28. #define lson idx<<1,l,mid
  29. #define rson idx<<1|1,mid+1,r
  30. #define PI 3.1415926535898
  31. template<class T> T min(const T& a,const T& b,const T& c) {
  32. return min(min(a,b),min(a,c));
  33. }
  34. template<class T> T max(const T& a,const T& b,const T& c) {
  35. return max(max(a,b),max(a,c));
  36. }
  37. void debug() {
  38. #ifdef ONLINE_JUDGE
  39. #else
  40.  
  41. freopen("d:\\in1.txt","r",stdin);
  42. freopen("d:\\out1.txt","w",stdout);
  43. #endif
  44. }
  45. int getch() {
  46. int ch;
  47. while((ch=getchar())!=EOF) {
  48. if(ch!=' '&&ch!='\n')return ch;
  49. }
  50. return EOF;
  51. }
  52.  
  53. const int maxn=;
  54. const int maxm=;
  55. int c[maxn],a[maxn];
  56. int dp[maxm];
  57.  
  58. int main()
  59. {
  60. //freopen("data.in","r",stdin);
  61. int n,m;
  62. while(cin>>n>>m)
  63. {
  64. if(n&&m);else break;
  65. for(int i=;i<=n;i++)
  66. cin>>a[i];
  67. for(int i=;i<=n;i++)
  68. cin>>c[i];
  69. memset(dp,-,sizeof(dp));
  70. dp[]=;
  71. for(int i=;i<=n;i++)
  72. {
  73. for(int j=;j<=m;j++)
  74. {
  75. if(dp[j]>=)
  76. dp[j]=c[i];
  77. else if(j>=a[i]&&dp[j-a[i]]>)
  78. dp[j]=dp[j-a[i]]-;
  79. }
  80. }
  81. int cnt=;
  82. for(int i=;i<=m;i++)
  83. if(dp[i]>=)
  84. cnt++;
  85. cout<<cnt<<endl;
  86. }
  87. return ;
  88. }

POJ 1742 Coins DP 01背包的更多相关文章

  1. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  2. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  3. Poj 1742 Coins(多重背包)

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

  4. POJ 1742 Coins(多重背包?)

    题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一 ...

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

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

  6. POJ 1742 Coins(多重背包) DP

    参考:http://www.hankcs.com/program/cpp/poj-1742-coins.html 题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些 ...

  7. poj 1742 Coins (多重背包)

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

  8. POJ 1742 Coins(多重背包,优化)

    <挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...

  9. POJ 1742 Coins 【可行性背包】【非原创】

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

随机推荐

  1. 建立一个简单的SpringMVC程序

    首先,所建立的程序是一个web程序,所以在web.xml文件中进行如下的配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  2. bzoj3087: Coci2009 misolovke

    Description [misolovke]给定一个 N*N 的网格.每个格子里至少会有一个捕鼠器, 并且已知每个格子里的捕鼠器个数.现在需要在 每一行 中选取恰好 K 个连续的格子, 把里面的捕鼠 ...

  3. rh6安装oracle11g+ASM

    安装步骤我这里略过,主要说下安装过程中遇到到问题或重要步骤: 1.UDEV绑定: for i in b c d e ;doecho "KERNEL==\"sd*\", B ...

  4. 使用Maven编译项目时提示程序包javax.servlet.http不存在

    将apache-tomcat-8.0.23\lib下的servlet-api.jar拷贝到C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext下即可

  5. crosswalk-webview

    https://github.com/crosswalk-project/cordova-plugin-crosswalk-webview https://cordova.apache.org/doc ...

  6. centos 6.3 安装 svn

    1. 按照http://ostechnix.wordpress.com/2013/04/30/install-subversion-server-in-centos-6-4-rhel-6-4-scie ...

  7. Thinkpad 笔记本VMware Workstation 安装虚拟机出现“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”解决方法

        今天在使用VMware打算在机器中安装新的虚拟机时,出现"此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态"错误如下:  提示信息: 已将该虚拟机配 ...

  8. C# 正则表达式及返回匹配的所有结果

    C# 正则表达式是在  System.Text.RegularExpressions 空间定义的,其中的 IsMatch() 方法用于返回 bool 类型,表示要搜索的字符串与传入的正则表达式是否匹配 ...

  9. 7,SFDC 管理员篇 - 数据模型 - 公式和验证 1

    1,自定义公式 Customize | Your Object | Fields | Add Fields Field SF的公式和Excel的公式差不多,都是支持各种运算和结果 例1,以opport ...

  10. display:none与visibility: hidden的区别

    display:none和visibility: hidden都能把网页上某个元素隐藏起来,但两者有区别: display:none ---不为被隐藏的对象保留其物理空间,即该对象在页面上彻底消失. ...