poj 1276 Cash Machine(多重背包)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 33444 | Accepted: 12106 |
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.
Source
一般解决方法:多重背包=01背包+完全背包
此处01处理时加上二进制思想,不然会超时
//524K 47MS C++ 742B
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std; int dp[];
int m[], d[];
int main(void)
{
int cash, n;
while(scanf("%d%d",&cash,&n)!=EOF)
{
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
scanf("%d%d",&m[i],&d[i]);
}
for(int i=;i<n;i++){
if(m[i]*d[i]>cash){
for(int j=d[i];j<=cash;j++)
dp[j] = max(dp[j], dp[j-d[i]] + d[i]);
}else{
int km=;
int k=m[i];
while(km<k){
int val = km*d[i];
for(int j=cash;j>=val;j--)
dp[j] = max(dp[j], dp[j-val] + val); k -= km;
km<<=;
} int val = k*d[i];
for(int j=cash;j>=val;j--)
dp[j] = max(dp[j], dp[j-val] + val);
}
}
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 多重背包及优化
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: 38986 Accepted: 14186 De ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 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) ...
随机推荐
- Qt编程之mapx组件编程
今天利用组件打包程序写了一些小例子,给大家贴出来,顺便提高一下记忆. 上一篇介绍了直接使用控件和使用控件打包程序对控件进行编程的方法.个人感觉后者编程更方便.不说废话了: 头文件中所需要的类型指针对象 ...
- Chrome 没有注册类
妈蛋这个问题折腾了好久,百度都是噼里啪啦什么什么进入注册表什么的删除啊 操 看着难受死了 这边也有这个些 没有注册类原因什么的的百度一下就有了 解决办法 管理员身份运行CMD 输入 REG DELE ...
- webpack使用笔记
webpack简介 CommonJS和AMD是用于JavaScript模块管理的两大规范,前者定义的是模块的同步加载,主要用于NodeJS:而后者则是异步加载,通过requirejs等适用于前端.np ...
- Centos Cacti 0.8.8g
一.Cacti简介1. cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户.因 ...
- 【MongoDB】MongoDB 3.2 SCRAM-SHA-1验证方式
新版本已取消addUser方法,改使用createUser方法 官方地址:https://docs.mongodb.com/manual/tutorial/create-users/ 官方地址:htt ...
- 安装spf13-vim on Windows10
安装之前 1.系统为 windows10 Version 1607 64位 企业版 2.参考 http://www.codeweblog.com/gvim-64%E4%BD%8D-windows-7 ...
- C++学习基础十一——子类对象向父类对象的转化
一.C++中可以实现子类向父类的转换,主要分为三种形式: 1.对象转换:不能实现动态绑定,即不能实现多态. 2.引用转换:动态绑定,实现多态. 3.指针转换:动态绑定,实现多态. 注意:一般不会出现父 ...
- 利用IDL将一个txt文档拆分为多个
测试.txt文档,每47行的格式相同,通过代码每47行存为一个txt,txt文档命名为其第一行数据. 代码如下: file='G:\data\测试.txt' openr,lun,file,/Get_L ...
- redis常用总结
1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...
- ActiveMQ实现负载均衡+高可用部署方案
一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现 2.activemq的特 ...