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(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

做这个题时,第一次这么写的:
#include<iostream>
#include<cstdio>
using namespace std;
bool dp[100][100001];
int n,m,a[100],su[100];
int main(){
while(cin>>n>>m){
if(n==0&&m==0) return 0;
for(int i=0;i<n;i++)
for(int j=1;j<=m;j++)
dp[i][j]=false;
for(int i=0;i<n;i++)
scanf("%d",&a[i]),dp[i][0]=true; //dp[i][j]为真,表示j元可以凑出来 ,a[i]硬币的面额
for(int i=0;i<n;i++)
scanf("%d",&su[i]); //su[i]表示面额为a[i]的硬币的数量
for(int i=1;i<=su[0]&&a[0]*i<=m;i++) //注意a[0]*i<=m,or就会出现runtime error
dp[0][a[0]*i]=true;
for(int i=1;i<n;i++){
for(int j=1;j<=m;j++){
for(int k=1;k<=su[i];k++){
if(j-k*a[i]>=0&&dp[i-1][j-k*a[i]]) dp[i][j]=true;
else dp[i][j]=dp[i-1][j];
}
}
}
int s=0;
for(int i=1;i<=m;i++){
if(dp[n-1][i]) /*printf("%d ",i),*/s++;
}
printf("%d\n",s);
}
}
这么写的话会超时,虽然这个dp好理解,但是这个dp的复杂度为O(m(su[i]的和))
现在附上第二次AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[2][100005];
int main(){
int n,m,va[102],su[102];
while(cin>>n>>m){
if(n==0&&m==0) return 0;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
scanf("%d",&va[i]); //硬币的面额
for(int i=0;i<n;i++)
scanf("%d",&su[i]); //面额为va[i]的硬币的数量
for(int i=0;i<=su[0]&&va[0]*i<=m;i++)
dp[0][va[0]*i]=su[0]-i;
for(int i=1;i<n;i++){
for(int j=0;j<=m;j++){
if(dp[(i-1)%2][j]>=0) dp[i%2][j]=su[i];
else if(j<va[i]||dp[i%2][j-va[i]]<=0) dp[i%2][j]=-1;
else dp[i%2][j]=dp[i%2][j-va[i]]-1;
}
}
int s=0;
for(int i=1;i<=m;i++)
if(dp[(n-1)%2][i]>=0) /*printf("%d ",i),*/s++;
printf("%d\n",s);
}
}
这个复杂度为O(nm),
dp[i][j]表示前i+1中硬币加和得到j元钱时,第i+1种硬币剩余的数量。

poj1742Coins(多重背包)的更多相关文章

  1. POJ1742Coins(多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32309   Accepted: 10986 Descripti ...

  2. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  3. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  6. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  7. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  8. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  9. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  10. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

随机推荐

  1. React项目 - 几种CSS实践

    前言团队在使用react时,不断探索,使用了很多不同的css实现方式,此篇blog总结了,react项目中常见的几种css解决方案:inline-style/radium/style-componen ...

  2. python学习第四十四天斐波那契数列和yield关键词使用

    斐波那契数列是数学中的常见的算法,第一个第二个不算,从第三个开始,每个数的都是前面两个数的和,使用yield关键词把生成的数列保存起来,调用的时候再调用,下面举例说明一下 def fab(max): ...

  3. C++11常用特性总结

    非原创,转载出处 http://www.cnblogs.com/feng-sc C++11已经出来很久了,网上也早有很多优秀的C++11新特性的总结文章,在编写本博客之前,博主在工作和学习中学到的关于 ...

  4. Sprak2.0 Streaming消费Kafka数据实时计算及运算结果保存数据库代码示例

    package com.gm.hive.SparkHive; import java.util.Arrays; import java.util.Collection; import java.uti ...

  5. linux强制用户下线命令

    linux强制用户下线命令   前提:必须是root权限操作:(1)使用who查看目前有哪些用户登录了服务器,见下图(2)使用pkill -kill -t pts/1命令踢出第一个用户.命令解释:pt ...

  6. Photoshop画笔工具的使用

    现在我们按下[B]从工具栏选择画笔工具,如果选中了铅笔就[SHIFT B]切换到画笔.然后按下[D],它的作用是将颜色设置为默认的前景黑色.背景白色.也可以点击工具栏颜色区的默认按钮(下左图红色箭头处 ...

  7. 前端每日实战:98# 视频演示如何用纯 CSS 创作一只愤怒小鸟中的绿猪

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VBGWqX 可交互视频 此视频是可 ...

  8. 黑客教父郭盛华:提升家庭WiFi的10个方法

    中国黑客教父,知名网络安全专家郭盛华曾发博文表示:“WiFi是互联网发展过程中最重要的发展之一,虚拟世界没有百分百的安全,所以杀毒软件并不可以抵抗全部的黑客攻击.“ 用户WiFi密码尽量不要使用简单单 ...

  9. windows下zookeeper集群安装

    windows下zookeeper单机版安装,见:https://www.cnblogs.com/lbky/p/9867899.html 一:zookeeper节点为什么是奇数个? 单机模式的zk进程 ...

  10. 批处理bat文件显示中文乱码解决方式

    1.下载Notepad++并安装 2.选择编码,将文件编码转换为ANSI编码