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 ...
随机推荐
- 那些有意思的Github
https://github.com/ngosang/trackerslist Linux下那些有趣的命令.. https://www.linu&xp&robe.com/linux-i ...
- Redis学习和应用记录(1)--介绍和安装
Redis是一个开源的分布式缓存框架,它也常被理解为数据结构服务器,因为它包含丰富的数据类型,如strings, hashes, lists, sets, sorted sets, bitmaps a ...
- ural 1012. K-based Numbers. Version 2(大数dp)
和1009相同,只是n达到了180位,可以模拟大数加和大数乘,这里用的java中的大数. import java.math.BigInteger; import java.util.Scanner; ...
- html5+css3杂记
H5C3个人笔记 before&after 1. 必须有content display 2. 场景:不想改变html结构:解决浮动 解决浮动: 2c d h v transition 过渡 1 ...
- win7 硬盘安装suse双系统启动顺序更改
使用win7硬盘安装suse双系统之后,首先面临的问题是,PC默认启动的系统更改的问题,有些人可能想默认启动是win7,只有在使用linux的时候在去选择suse系统,这里我告诉大家更改的办法: 首先 ...
- c++将bool变量以文字形式打印
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...
- nc的简单使用
1.传输文件: 目的主机监听 nc -l 监听端口<未使用端口> > 要接收的文件名 nc -l 6666 > filename.tar 源主机发起请求 nc 目的主机ip ...
- Lazarus 1.6 增加了新的窗体编辑器——Sparta_DockedFormEditor.ipk
一下是该控件官网的介绍 "Hello A package for a docked form editor can be found in : components/sparta/docke ...
- C# 统计字符串出现的个数
string str1 = "123AAA456AAAA789AAAAAAA1011"; string str2 = "123456789AAA23456789AAAA3 ...
- 如何将本地项目上传到gitlab上?
git push后需要输入用户名,密码 这是上传成功显示的页面 打开gitLab项目地址检查代码是否被正确上传 上传前: 上传后 这就完成将项目上传到gitlab了