poj1742Coins(多重背包)
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(多重背包)的更多相关文章
- POJ1742Coins(多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32309 Accepted: 10986 Descripti ...
- 洛谷P1782 旅行商的背包[多重背包]
题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...
- HDU 2082 找单词 (多重背包)
题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- POJ1742 Coins[多重背包可行性]
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 34814 Accepted: 11828 Descripti ...
- POJ1276Cash Machine[多重背包可行性]
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32971 Accepted: 11950 De ...
随机推荐
- OSI模型——传输层
OSI模型——传输层 运输层 运输层概述 运输层提供应用层端到端通信服务,通俗的讲,两个主机通讯,也就是应用层上的进程之间的通信,也就是转换为进程和进程之间的通信了,我们之前学到网络层,IP协议能将分 ...
- 问题 A: 组合数
问题 A: 组合数 时间限制: 1 Sec 内存限制: 128 MB提交: 1975 解决: 150[提交] [状态] [命题人:jsu_admin] 题目描述 求组合数C(N,M),以及C(N, ...
- Ubuntu 安装 ansible
sudo apt update sudo apt-get install software-properties-common sudo apt-add-repository --yes ppa:an ...
- JAVA对象序列化和反序列化学习
JAVA序列化就是将JAVA对象转化为字节序列的过程,而JAVA反序列化就是将字节序列转化为JAVA对象的过程. 这一过程是通过JAVA虚拟机独立完成,所以一个对象序列化后可以在任意时间和任意机器上反 ...
- [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (2/5)
- CentOS7安装MongoDB及基础操作
安装环境说明 系统环境说明 [root@master ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@ma ...
- 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。
将项目的平台目标改为“Any CPU” 在项目上右击选择属性——>生成——>平台目标 选择Any CPU
- 2018-9-21-dot-net-core-使用-usb
title author date CreateTime categories dot net core 使用 usb lindexi 2018-09-21 19:53:34 +0800 2018-0 ...
- [python 学习] lambda
#!/usr/bin/python # -*- encoding:utf-8 -*- # lambda parameter_list: expression f = lambda x,y: x + y ...
- django之静态文件的设置
一:静态文件 Django中提供了一种解析的方式配置静态文件路径.静态文件可以放在项目根目录下,也可以放在应用的目录下,由于有些静态文件在项目中是通用的,所以推荐放在项目的根目录下,方便管理. 为了提 ...