题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2844

题目大意:

Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[i]要买的物品价值不超过m

输入:第一行输入n和m,第二行输入n个硬币的面值和n个硬币的数量,输入0 0结束

输出:1到m之间有多少价格Tony可以支付

思路:

多重背包修改一下,如果dp[j-w[i]]可以到达,那么dp[j]也可到达

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. const int INF = 0x3f3f3f3f;
  6. const int maxn = + ;
  7. int T, n, m, cases;
  8. int cost[maxn], amount[maxn];
  9. bool dp[ + ];
  10. void zeroone(int cost)
  11. {
  12. for(int i = m; i >= cost; i--)
  13. if(dp[i - cost])dp[i] = dp[i - cost];
  14. }
  15. void complete(int cost)
  16. {
  17. for(int i = cost; i <= m; i++)
  18. if(dp[i - cost])dp[i] = dp[i - cost];
  19. }
  20. void solve(int cost, int amount)
  21. {
  22. if(cost * amount >= m)
  23. complete(cost);
  24. else
  25. {
  26. int k = ;//二进制优化
  27. while(k <= amount)
  28. {
  29. zeroone(k * cost);
  30. amount -= k;
  31. k *= ;
  32. }
  33. zeroone(amount * cost);
  34. }
  35. }
  36. int main()
  37. {
  38. while(cin >> n >> m && (n + m))
  39. {
  40. for(int i = ; i < n; i++)cin >> cost[i];
  41. for(int i = ; i < n; i++)cin >> amount[i];
  42. memset(dp, , sizeof(dp));
  43. dp[] = ;
  44. for(int i = ; i < n; i++)
  45. {
  46. solve(cost[i], amount[i]);
  47. }
  48. int ans = ;
  49. for(int i = ; i <= m; i++)ans += dp[i];
  50. cout<<ans<<endl;
  51. }
  52. return ;
  53. }

hdu-2844&&POJ-1742 Coins---多重背包的更多相关文章

  1. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  2. 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  3. POJ 1742 Coins(多重背包, 单调队列)

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

  4. POJ 1742 Coins (多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Descriptio ...

  5. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  6. HDU 2844 二进制优化的多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. poj 1742 coins_多重背包

    题意:给你N个种硬币,价值和数量,知道手表不大于m,问能组成(1~m)的价格有多少种情况 套套上次那题的模板直接就行了,http://blog.csdn.net/neng18/article/deta ...

  8. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  9. HDU 3732 Ahui Writes Word(多重背包)

    HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...

  10. poj 1742 Coins (多重背包)

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

随机推荐

  1. php composer php amqplib

    下载composer.phar放到项目根目录 d:/www/apps https://getcomposer.org/download/1.4.1/composer.phar 下载安装Composer ...

  2. Python 错误总结

    1.以一种访问权限不允许的方式做了一个访问套接字的尝试. 解决方法:这个问题缘由是有端口被占用

  3. python模块之numpy

    Numpy是一个第三方库,是数组相关的运算 通过pip安装:pip install numpy Anaconda python的一个科学计算发行版本,安装后将不必单独安装numpy,下面的库模块也将不 ...

  4. tcp/ip 性能优化问题的思考学习

    首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层.  其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Data)在 ...

  5. Turing Year 2012

    Turing LectureFrom cryptanalysis to cognitive neuroscience - a hidden legacy of Alan Turinghttp://co ...

  6. 性能测试工具Jmeter07-Jmeter性能测试实战

    测试需求:测试20个用户访问www.baozhenart.com在负载达到30QPS时的平均响应时间 QPS:Query Per Second每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特 ...

  7. [转]javascript实现限制上传文件的大​​小

    本文转自:http://www.micmiu.com/lang/javascript/js-check-filesize/ 目录 基本思路 示例 [一].基本思路 在FireFox.Chrome浏览器 ...

  8. Erlang C 與M/M/N排隊模型

    一何谓排队模型 在现实生活中排队的现象可说是无处不在,如:买票.超商.百货公司…等.顾客总是在揣测"排在哪一个服务台会比较快?"或"到底还要排多久呢?"类似这样 ...

  9. node Error: Could not locate the bindings file. Tried:解决

    问题描述: Error: Could not locate the bindings file. Tried: → C:\code\xxx\node_modules\deasync\build\dea ...

  10. MVC数据库问题(更新,添加字段)

    1.更新模型之后,在"程序包管理控制器"中输入Update-database,就能自动完成更新 2.对于Update-database之后报错:Automatic migratio ...