hdu-2844&&POJ-1742 Coins---多重背包
题目链接:
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]也可到达
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int maxn = + ;
- int T, n, m, cases;
- int cost[maxn], amount[maxn];
- bool dp[ + ];
- void zeroone(int cost)
- {
- for(int i = m; i >= cost; i--)
- if(dp[i - cost])dp[i] = dp[i - cost];
- }
- void complete(int cost)
- {
- for(int i = cost; i <= m; i++)
- if(dp[i - cost])dp[i] = dp[i - cost];
- }
- void solve(int cost, int amount)
- {
- if(cost * amount >= m)
- complete(cost);
- else
- {
- int k = ;//二进制优化
- while(k <= amount)
- {
- zeroone(k * cost);
- amount -= k;
- k *= ;
- }
- zeroone(amount * cost);
- }
- }
- int main()
- {
- while(cin >> n >> m && (n + m))
- {
- for(int i = ; i < n; i++)cin >> cost[i];
- for(int i = ; i < n; i++)cin >> amount[i];
- memset(dp, , sizeof(dp));
- dp[] = ;
- for(int i = ; i < n; i++)
- {
- solve(cost[i], amount[i]);
- }
- int ans = ;
- for(int i = ; i <= m; i++)ans += dp[i];
- cout<<ans<<endl;
- }
- return ;
- }
hdu-2844&&POJ-1742 Coins---多重背包的更多相关文章
- hdu 2844 poj 1742 Coins
hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...
- 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- POJ 1742 Coins (多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 28448 Accepted: 9645 Descriptio ...
- Coins HDU - 2844 POJ - 1742
Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...
- HDU 2844 二进制优化的多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- poj 1742 coins_多重背包
题意:给你N个种硬币,价值和数量,知道手表不大于m,问能组成(1~m)的价格有多少种情况 套套上次那题的模板直接就行了,http://blog.csdn.net/neng18/article/deta ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- HDU 3732 Ahui Writes Word(多重背包)
HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...
- poj 1742 Coins (多重背包)
http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...
随机推荐
- php composer php amqplib
下载composer.phar放到项目根目录 d:/www/apps https://getcomposer.org/download/1.4.1/composer.phar 下载安装Composer ...
- Python 错误总结
1.以一种访问权限不允许的方式做了一个访问套接字的尝试. 解决方法:这个问题缘由是有端口被占用
- python模块之numpy
Numpy是一个第三方库,是数组相关的运算 通过pip安装:pip install numpy Anaconda python的一个科学计算发行版本,安装后将不必单独安装numpy,下面的库模块也将不 ...
- tcp/ip 性能优化问题的思考学习
首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Data)在 ...
- Turing Year 2012
Turing LectureFrom cryptanalysis to cognitive neuroscience - a hidden legacy of Alan Turinghttp://co ...
- 性能测试工具Jmeter07-Jmeter性能测试实战
测试需求:测试20个用户访问www.baozhenart.com在负载达到30QPS时的平均响应时间 QPS:Query Per Second每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特 ...
- [转]javascript实现限制上传文件的大小
本文转自:http://www.micmiu.com/lang/javascript/js-check-filesize/ 目录 基本思路 示例 [一].基本思路 在FireFox.Chrome浏览器 ...
- Erlang C 與M/M/N排隊模型
一何谓排队模型 在现实生活中排队的现象可说是无处不在,如:买票.超商.百货公司…等.顾客总是在揣测"排在哪一个服务台会比较快?"或"到底还要排多久呢?"类似这样 ...
- 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 ...
- MVC数据库问题(更新,添加字段)
1.更新模型之后,在"程序包管理控制器"中输入Update-database,就能自动完成更新 2.对于Update-database之后报错:Automatic migratio ...