Cash Machine(多重背包)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24067 | Accepted: 8414 |
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.
#include<stdio.h>
#include<string.h> int cash,n;
int num[],p[],use[];
bool dp[]; void MultiplePack()
{
memset(dp,,sizeof(dp));
dp[] = ;//0元肯定能达到,标记为达到;
for(int i = ; i <= n; i++)
{
memset(use,,sizeof(use));//第i种面值的使用次数初始化为0;
for(int j = p[i]; j <= cash; j++)
{
if(!dp[j] && dp[j-p[i]] && use[j-p[i]] < num[i])//钱数j能达到,并且面值i的使用次数不超过num[i];
{
dp[j] = ;//标记能达到
use[j] = use[j-p[i]] + ;//面值i的使用次数加1;
}
}
}
for(int i = cash; i >= ; i--)
{
if(dp[i])
{
printf("%d\n",i);
break;
}
}
}
int main()
{
while(~scanf("%d",&cash))
{
scanf("%d",&n);
for(int i = ; i <= n; i++)
scanf("%d %d",&num[i],&p[i]);
MultiplePack();
}
return ;
}
Cash Machine(多重背包)的更多相关文章
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
- POJ-1276 Cash Machine 多重背包 二进制优化
题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...
- PKU--1267 Cash Machine(多重背包)
题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
随机推荐
- 隐藏iframe边框
关于iframe在ie浏览器中边框隐藏的问题,一直困惑着我.其实就是一个很简单的办法,主要设置一个属性即可解决,方法如下: <iframe frameborder="0"&g ...
- eclipse-SDK-3.7-win32;eclipse-java-indigo-win32;eclipse-jee-indigo-win32 区别(ZZ)
eclipse-SDK-3.7-win32:eclipse-java-indigo-win32:eclipse-jee-indigo-win32 三个都是用于win32,即windows系统的32位机 ...
- Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用
1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...
- kafka版本0.8.2.0-Producer Configs之request.required.acks
This value controls when a produce request is considered completed. Specifically, how many other bro ...
- C#重载重写
overload:重载指的是同一个类中有两个或多个名字相同但是参数不同的方法,(注:返回值不能区别函数是否重载),重载没有关键字.override:过载也称重写是指子类对父类中虚函数或抽象函数的“覆盖 ...
- Android 控件 之 Menu 菜单
http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu (选项菜单)用法总结 使用方法: 方法一:添 ...
- Android之提交数据到服务端方法简单封装
在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...
- Android App优化建议(转载)
假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗电或者内存占用等 ...
- 关于ASP.NET控件方面的学习(恢复版)
前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...
- 比较两份文件的异同-beyond compare
比较两份文件的异同-beyond compare 今天又学到一个知识,比较两次文件的 异同. 哈哈 谢谢领导