[poj 1276] Cash Machine 多重背包及优化
Description
N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10
means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.
Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.
Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 
Input
cash N n1 D1 n2 D2 ... nN DN
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct.
Output
Sample Input
735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
Sample Output
735
630
0
0
Hint
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = ;
int cash, N, cnt[], d[];
int dp[Max]; void ZeroOnePack(int d)
{
for (int i = cash; i >= d; i--)
dp[i] = max(dp[i], dp[i-d]+d);
} void CompletePack(int d)
{
for (int i = d; i <= cash; i++)
dp[i] = max(dp[i], dp[i-d]+d);
} void MultiplePack(int m, int d)
{
if (m*d >= cash) {
CompletePack(d);
return ;
}
int k = ;
while (k < m) {
ZeroOnePack(k*d);
m -= k;
k *= ;
}
ZeroOnePack(m*d);
} int main()
{
//freopen("1.txt", "r", stdin);
while(cin >> cash) {
cin >> N;
for (int i = ; i <= N; i++)
cin >> cnt[i] >> d[i];
memset(dp, , sizeof(dp));
for (int i = ; i <= N; i++)
MultiplePack(cnt[i], d[i]);
printf("%d\n", dp[cash]);
} return ;
}
参考资料:背包九讲
[poj 1276] Cash Machine 多重背包及优化的更多相关文章
- POJ 1276 Cash Machine(多重背包的二进制优化)
		题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ... 
- 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: 38986 Accepted: 14186 De ... 
- Cash Machine (POJ 1276)(多重背包——二进制优化)
		链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ... 
- POJ-1276 Cash Machine 多重背包 二进制优化
		题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ... 
- poj 1276 Cash Machine_多重背包
		题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ... 
- 【转载】poj 1276 Cash Machine  【凑钱数的问题】【枚举思路 或者 多重背包解决】
		转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ... 
- poj 1276 Cash Machine(多重背包)
		Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ... 
- POJ 1276:Cash Machine 多重背包
		Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ... 
随机推荐
- 用一句SQL取出第 m 条到第 n 条记录的方法
			1 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)2 3 SELECT TOP n-m+1 * 4 FROM Table 5 WHERE (id NOT IN (SEL ... 
- node.js抓取网上图片保存到本地
			用到两个模块,http和fs var http = require("http");var fs = require("fs"); var server = h ... 
- Java之泛型浅解
			我觉得学习一个东西,首先得从概念上明白它大概是什么? “泛型”就是“参数化类型”,也就是是把类型当成了一种参数.之前我们看到得函数方法比如: public long add(int num1,int ... 
- spring事务隔离级别以及脏读 不可重复读 幻影读
			隔离级别 声明式事务的第二个方面是隔离级别.隔离级别定义一个事务可能受其他并发事务活动活动影响的程度.另一种考虑一个事务的隔离级别的方式,是把它想象为那个事务对于事物处理数据的自私程度. 在一个典型的 ... 
- Python基础知识之字符串操作方法总结
			Python 中字符串也是一种数据类型,针对此数据总结下常用的方法 1,字符串截取,变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾 ... 
- java支付宝开发-异常-01-"sub_code":"isv.invalid-app-id","sub_msg":"无效的AppID参数"
			一.现象 无论请求哪个接口都报这个错误 二.异常原因 后来检查了一下,发现是因为 我支付宝网关写错了.沙箱环境和正式环境 的支付宝网关不同,如下 //支付宝网关名-正式环境 //public stat ... 
- nodejs stream & buffer 互相转换
			stream 转 buffer function streamToBuffer(stream) { return new Promise((resolve, reject) => { let b ... 
- linux命令学习笔记(60):scp命令
			scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行 拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务 ... 
- 【leetcode刷题笔记】Flatten Binary Tree to Linked List
			Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ... 
- buildroot mysql mysql.mk hacking
			/*********************************************************************** * buildroot mysql mysql.mk ... 
