Cash Machine
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30006   Accepted: 10811

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply
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

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 



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

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

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

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 



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 多重背包的更多相关文章

  1. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  2. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  3. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  4. poj 1276 Cash Machine_多重背包

    题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...

  5. 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

    转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...

  6. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  7. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  8. Cash Machine (POJ 1276)(多重背包——二进制优化)

    链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...

  9. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

随机推荐

  1. ch4 圆角框

    固定宽度的圆角框 只需要两个图像:一个应用于框的顶部,一个应用于底部 <div class="box"> <h2>Lorem Ipsum</h2> ...

  2. Python 3网络爬虫开发实战书籍

    Python 3网络爬虫开发实战书籍,教你学会如何用Python 3开发爬虫   本书介绍了如何利用Python 3开发网络爬虫,书中首先介绍了环境配置和基础知识,然后讨论了urllib.reques ...

  3. Redis散列表类型

    散列类型(hash)的键值也是一种字典结构,其存储了字段(field)和字段值的映射,但字段值只能是字符串,不支持其他的数据类型. 一个散列类型键可以包含至多2^32 -1个字段. 命令 赋值 HSE ...

  4. 通过SparkListener监控spark应用

    监控spark应用的方式比较多,比如spark on yarn可以通过yarnClient api监控.这里介绍的是spark内置的一种监控方式 如果是sparkStreaming,对应的则是stre ...

  5. py交易

    下载之后发现是pyc文件,反编译一下 网站:https://tool.lu/pyc/ 发现其实就是base64转换为16进制,然后减去16再和32异或,就可以得到答案了 nctf{d3c0mpil1n ...

  6. Day7 - C - Saddle Point ZOJ - 3955

    Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbe ...

  7. Java生鲜电商平台-优惠券系统设计详解

    Java生鲜电商平台-优惠券系统设计详解 优惠券作为电商最常用的营销手段,对于商家而言可以起到拉新.促活.提高转化的作用,对用户而言也可以获得实惠,今天就来谈谈优惠券系统的设计逻辑. 我对于优惠券系统 ...

  8. 040、Java中逻辑运算之短路与运算“&&”

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  9. notifix测试

    成功 失败 警告 提示

  10. 编译安装常用包+阿里镜像源-常用资源-系统-下载-科莱软件下载-docker仓库包-安全圈-杏雨梨云-图形界面安装-docker私有双仓库-阿里源报错处理-centos7目录大小

    yum install apr-util apr-util-devel apr apr-devel pcre pcre-devel zlib zlib-devel openssl openssl-de ...