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 ...
随机推荐
- 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap
https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...
- 洛谷 - P3649 - 回文串 - 回文自动机
https://www.luogu.org/problem/P3649 #include <bits/stdc++.h> using namespace std; typedef long ...
- H5白屏问题
前言 前阵子弄了灰度环境,H5这边需要给灰度环境的接口加上Cookie,配置的期间遇到一些Cookie问题以及白屏在此记录下 1.H5请求接口带不上Cookie 解决方法:前端使用了 webpack ...
- menustrip
在对应菜单上点击鼠标右键,插入,SEPARATOR 就可以了,然后可以选中拖动位置.
- 360CTF Re wp
这比赛唯一的一道Re
- sparkStreaming复习笔记(1)
一.SparkStreaming 1.sparkcore模块的扩展,具有可扩展,高吞吐量,容错机制,针对实时数据流处理,数据可以来自于kafka,flume以及tcp套接字,可以使用更加复杂的函数来进 ...
- Oracle Grid,ASM,Database on Redhat 7.5
目录 Oracle安装包 Oracle官方文档 Blog Oracle Grid Installation Process 用户.组.目录 Oracleasm 创建 ASM 磁盘 Database S ...
- union 横向组合
select sum(zs) zs,sum(zl) zl,sum(ts) ts,sum(lxcbw) lxcbw,sum(bz) bz,sum(sfzqt) sfzqtfrom (select cou ...
- Hugin
Hugin简介 Hugin是一个开源的拼接软件,包含大量的拼接所需模块源码以及使用了部分Panorama Tools中的工具. 1)libpano13(Panorama Tools). 2)cpfin ...
- Linux telnet、nc、ping监测状态
在工作中会遇到网络出现闪断丢包的情况,最终影响业务工常使用.可以业务服务器上发起监测. 1.通过telnet echo -e "\n" | telnet localhost 2 ...