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

Source Code

Problem:         User: BMan
Memory: 1112K Time: 1547MS
Language: G++ Result: Accepted
Source Code
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("d:\\in1.txt","r",stdin);
freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn=;
const int maxm=;
int c[maxn],a[maxn];
int dp[maxm]; int main()
{
//freopen("data.in","r",stdin);
int n,m;
while(cin>>n>>m)
{
if(n&&m);else break;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
cin>>c[i];
memset(dp,-,sizeof(dp));
dp[]=;
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]]>)
dp[j]=dp[j-a[i]]-;
}
}
int cnt=;
for(int i=;i<=m;i++)
if(dp[i]>=)
cnt++;
cout<<cnt<<endl;
}
return ;
}

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. PostMessager来对子父窗体进行跨域

    一.为什么需要使用postMessage这个跨域技术 对于一个普通的页面而言,如果页面中的数据量太多时,会导致某个页面的数据量太多 二显得特别的臃肿,所以通常是使用iframe的方式来加载子页面,但是 ...

  2. Linux 组群账户管理

    一.Linux组群账户配置文件 1./etc/group文件 /etc/group文件是用户组群的配置文件,内容包括用户和用户组群,并且能显示出用户是归属哪个用户组群或哪几个用户组群.一个用户可以归属 ...

  3. QTP实现功能测试的时候,当新版本的页面都改变了,应该如何解决

    去更改对象仓库的属性和更改对象仓库.

  4. Appium在没有收到下一个命令时,默认超时时间是60s,超时后应用将会自动关闭,如果有需要等待超过60s的场景,怎么处理?

    DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("newCo ...

  5. Sass浅谈

    对于一名前端开发来说,CSS并不陌生,几乎每天都在和CSS打交道.相处久了就会觉得CSS有些许的机械化,有些许的无趣:就会觉得写CSS很多时候都是在做一些复制粘贴性的工作,布局排版,颜色设置,边框属性 ...

  6. jquery 点击事件

    bind() 向匹配元素附加一个或更多事件处理器 blur() 触发.或将函数绑定到指定元素的 blur 事件 change() 触发.或将函数绑定到指定元素的 change 事件 click() 触 ...

  7. JS适配问题。

    动画requestAnimFrame + cancelAnimationFrame window.requestAnimFrame = (function(){ return window.reque ...

  8. EXCL poi导入

    public static void importExcel2(File file) throws Exception { InputStream is = new FileInputStream(f ...

  9. Inside The C++ Object Model - 02

    前言 - 什么是C++对象模型 C++对象模型包括2个方面的含义: 1.语言中直接支持面向对象程序设计的部分 2.对于各种(面向对象)支持的底层实现机制. 无论是什么语言,都需要转换为汇编.很多面向对 ...

  10. 今天遇到的关于mysql的max_allowed_packet的问题

    今天,运维组的同学来找我,说是备份池的文件描述没有显示出来,而且是从20号开始就不能显示,之前的文件描述就能显示,而且20号他们上传备份的数据确实是传过来的.但是是在web界面文件描述显示不出来. 先 ...