Cash Machine POJ 1276 多重背包
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35387 | Accepted: 12816 |
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100509
#define N 21
#define MOD 1000000
#define INF 1000000009
#define eps 0.00000001
/*
两种思路
1.看作多个0 1 背包问题
2.利用二进制分解 优化
*/
int value[N], dp[MAXN], num[N];
int aim, n, k;
void solve(int &Max, int num, int value)
{
for (int i = Max; i >= ; i--)
{
if (dp[i])
{
for (int k = ; k <= num; k++)
{
if (i + value*k>aim) continue;
dp[i + value*k] = ;
if (i + value*k > Max)
{
Max = i + value*k;
}
}
}
}
}
int main()
{
while (scanf("%d%d", &aim,&n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d%d", &num[i], &value[i]);
if (n == || aim == )
{
printf("0\n");
continue;
}
memset(dp, , sizeof(dp));
dp[] = ;
int ans = ;
for (int i = ; i < n; i++)
solve(ans, num[i], value[i]);
printf("%d\n", ans);
}
return ;
}
Cash Machine POJ 1276 多重背包的更多相关文章
- Cash Machine POJ - 1276 多重背包二进制优化
题意:多重背包模型 n种物品 每个m个 问背包容量下最多拿多少 这里要用二进制优化不然会超时 #include<iostream> #include<cstdio> #in ...
- poj 1276 多重背包
735 3 4 125 6 5 3 350 //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 ...
- Cash Machine POJ - 1276
解法 多重背包板子题 多重背包板子 如果上限的体积大于了给定的体积那么套完全背包 否则二进制优化成01背包 代码 #include <iostream> #include <cstr ...
- POJ 1276 (多重背包) Cash Machine
题意: 有n种纸币,已知每种纸币的面值和数量,求所能凑成的不超过cash的最大总面值. 分析: 这道题自己写了一下TLE了,好可耻.. 找了份比较简洁的代码抄过来了..poj1276 #include ...
- poj 2392 多重背包
题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- poj 1742 多重背包
题意:给出n种面值的硬币, 和这些硬币每一种的数量, 要求求出能组成的钱数(小于等于m) 思路:一开始直接用多重背包套上去超时了,然后就没辙了,然后参考网上的,说只需要判断是否能取到就行了,并不需要记 ...
- poj 1014多重背包
题意:给出价值为1,2,3,4,5,6的6种物品数量,问是否能将物品分成两份,使两份的总价值相等. 思路:求出总价值除二,做多重背包,需要二进制优化. 代码: #include<iostream ...
- Dividing POJ - 1014 多重背包二进制优化
多重背包模型 写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include&l ...
随机推荐
- Java的安装过程
记录一下自己在Windowns下安装java的过程 安装网址:http://www.oracle.com/index.html 打开网址后要先登录,如果没有号就先注册,然后才能下载 step1:下载J ...
- 枚举详解之EnumSet、EnumMap用法
枚举简单例子 /** * @author shuliangzhao * @Title: Color * @ProjectName design-parent * @Description: TODO ...
- Akka源码分析-Remote-ActorSystem
前面的文章都是基于local模式分析的,现在我们简要分析一下在remote模式下,ActorSystem的创建过程. final val ProviderClass: String = setup.g ...
- Java 开源博客 Solo 1.3.0 发布 - Docker 支持
Solo 1.3.0 正式发布了,感谢一直以来关注 B3log 开源的朋友! 可以通过一个命令启动(不需要安装数据库.部署容器),也可以通过 war 方式部署容器,连接 MySQL.这应该是史上最容易 ...
- Microsoft SQL Server学习(一)--基本概念
数据库的分类 关系型数据库 非关系型数据库 数据库的发展 数据都是以文件的形式存储在硬盘上FATFAT32NTFS LinuxEXT 数据库设计流程 文件系统缺陷 数据库的专业术语 数据库引擎服务 E ...
- python自动化测试框架(一)
1.开发环境 名称 版本 系统 windows 7 python版本 2.7.14 IDE pycharm2017 2.大致框架流程 :展示了框架实现的业务流程 3.框架介绍 3.1 ======完善 ...
- jQuery实现页面锚点滚动效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c++中std::set自定义去重和排序函数
c++中的std::set,是基于红黑树的平衡二叉树的数据结构实现的一种容器,因为其中所包含的元素的值是唯一的,因此主要用于去重和排序.这篇文章的目的在于探讨和分享如何正确使用std::set实现去重 ...
- SQLServer 异常捕获,回滚,再抛出
一个存储过程中多个更新操作,后面的更新操作出现异常,如果不手动回滚前面修改的数据是不会自动撤销的! BEGIN TRY BEGIN TRAN -- ..... COMMIT TRAN END TRY ...
- Postfix 故障记录
1.postfix 目录/var/mail/USER文件大小限制报错 解决方式: 编辑 /etc/postfix/main.cf 文件添加以下内容 mailbox_size_limit = 51200 ...