POJ 1276:Cash Machine 多重背包
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 30006 | Accepted: 10811 |
Description
of nk bills. For example,
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.
题意是给你几种金钱的面额,每种面额有多少张纸币,然后给一个target金额。问在给定的纸币条件下,能凑成的最大的小于等于target的面额是多少。
关于多重背包,背包九讲里是这么解答处理一件多重背包物品的过程的:
def MultiplePack(F,C,W,M)
if C * M >= V
CompletePack(F,C,W)
return
k := 1
while k < M
ZeroOnePack(kC,kW)
M := M - k
k := 2k
ZeroOnePack(C * M, W * M)
我的理解:首先对于一件物品来说,如果这个物品的容量*数目已经大于背包的总容量了,那么这与完全背包问题没有区别了。因为完全背包问题就是可以任意的往背包里面放物品,而这时这个物品的容量*数目已经大于背包总容量了,所以,也就相当于在背包总容量的范围内,这个物品是可以支持 任意地往背包里面放物品。
如果这个物品的容量*数目小于背包的总容量,那么正常情况下,需要一件一件往里面放找最大值的。但是它的方法是:将第i种物品分成若干件01背包中的物品,其中每件物品有一个系数。这件物品的费用和价值均是原来的费用和价值乘以这个系数。令这些系数分别为1,2, 2的2次方,2的三次方,。。。2的k-1次方,最后是Mi - 2的k次方 + 1,且k是满足Mi - 2的k次方 + 1 的最大整数。例如,如果Mi为13,则相应的k=3,这种最多取13件物品的应被分成系数分别为1
2 4 6 的四件物品。
我觉得这里设计的很nice在于,正常情况下,举例来说这个物品有13件,我们需要一件一件放,放1件,2件,3件,。。。13件都放入,然后不断找最大值。但是上面的方法利用二进制,4个数,就做了13个数的事情。
发现 1 2 4 6 这四个数相互之间的和 就已经可以表示1到13的所有数,原因就在于当我放入1件物品,再放入2件物品找最大值的时候,实际上相当于我一下子放入了该物品的3件,所以实际上3是不需要我们再去实验的了。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int w, n;
int num[500002];
int val[500002];
int c[500002];
long long dp[500002]; void Zero_Pack(int weight, int cost, int w)
{
int i;
for (i = w; i >= cost; i--)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} void Complete_Pack(int weight,int cost,int w)
{
int i;
for (i = cost; i <= w; i++)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} long long Multi_Pack(int c[],int val[],int num[],int n,int w)
{
memset(dp, 0, sizeof(dp));
int i;
for (i = 1; i <= n; i++)
{
if (num[i] * c[i] > w)
Complete_Pack(val[i],c[i],w);
else
{
int k = 1;
while (k < num[i])
{
Zero_Pack(k*val[i],k*c[i],w);
num[i] = num[i] - k;
k = k << 1;
}
Zero_Pack(num[i]*val[i],num[i]*c[i],w);
}
}
return dp[w];
}
int main()
{
int i, j, v, max_v,re;
while (scanf("%d%d", &n,&w) != EOF)
{
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; i++)
{
scanf("%d%d%d", c + i, val + i, num + i);
}
re = Multi_Pack(c, val, num, n, w);
cout << re << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
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 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- 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: 38986 Accepted: 14186 De ...
- Cash Machine (POJ 1276)(多重背包——二进制优化)
链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
随机推荐
- MySQL复制(一)--复制概述
MySQL复制(replication)文档集合:1.复制概述2.基于二进制日志文件位置(binlog)配置复制3.基于全局事物标识符(GTID)配置复制4.多源复制5.级联复制6.半同步复制7.延迟 ...
- 嵊州普及Day4T2
题意:有一个单行走廊,每回合第ai个展柜会冒出来一只鬼,右边尽头有一个人间大炮和向最左传送门(费用均1金币),你需要每回合将所有鬼交换展柜,全部至最右,问若从一到n所有回合结束是需多少金币可射死所有鬼 ...
- Codeforces Round #568 (Div. 2) 选做
A.B 略,相信大家都会做 ^_^ C. Exam in BerSU 题意 给你一个长度为 \(n\) 的序列 \(a_i\) .对于每个 \(i\in [1,N]\) 求 \([1,i-1]\) 中 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮组
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 使用 Helm【转】
Helm 安装成功后,可执行 helm search 查看当前可安装的 chart. 这个列表很长,这里只截取了一部分.大家不禁会问,这些 chart 都是从哪里来的? 前面说过,Helm 可以像 a ...
- centos7下yourcompleteme安装
以前装过一回,没成功,现在再来一次 yourcompleteme git https://github.com/ycm-core/YouCompleteMe#installation 检查软件版本 v ...
- Google宣布安全奖励项目(GPSRP)覆盖上亿的 Android 应用程序
自 2010 年推出除虫赏金项目以来,谷歌已经向安全研究人员支付了超过 1500 万美元的奖励.今天,这家科技巨头宣布进一步拓展 Google Play 安全奖励项目(GPSRP)的范围,以覆盖上亿的 ...
- SChema 多个属性的设置学习
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http:/ ...
- impala invalidate metadata和impala-shell -r作用相同
impala的invalidate metadata内部命令,是否和外部命令impala-shell -r的作用相同的? 这个问题的回答: 在invalidate metadata 和 impala- ...
- vSphere HA 原理与配置
内容预览: 1. vSphere HA 概述 2. vSphere HA 提供的保护级别 3. vSphere HA运行原理 4. vSphere HA 故障支持场景 5. vSphere HA接入控 ...