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) ...
随机推荐
- Tiny_4412的NFS挂载
设备连接: 交叉网线 串口线 usb线 电源线 网络设置: 笔记本ubuntu:wifi处,设置IPv4设置“仅本地连接”,不要 ...
- CRM客户关系管理系统 北京易信软科信息技术有限公司
北京易信软科信息技术有限公司 推出大型erp系统,库存管理系统,客户关系管理系统,车辆登记管理系统,员工管理系统,采购管理系统,销售管理系统,为您的企业提供最优质的产品服务 北京易信软科您可信赖的北京 ...
- 安装过程中出现PKG_CONFIG_PATH的问题解决方法
在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接提示没有安装某某库, 但是我们已经安装了啊. 其实造成这种情况的原因很简单, 就是安装的目录不能被依赖程序找到 ...
- AX7: How to deploy a Package
A. Using LCS services. B. Manual using command prompt. Here I’ll show using command prompt, as I fou ...
- C#笔试(程序设计)
1.如何把一个Array复制到ArrayList里,如何把ArrayList复制到Array里? foreach( object o in array )arrayList.Add(o); Array ...
- leetcode 205
205. Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...
- 这是个简单的UTF8转码的小Demo
NSString *name = @"你好啊"; NSString *string = [NSString stringWithFormat:@"%@",nam ...
- 【c++】虚基类
何要使用虚基类: 为何避免多层继承中出项多个公共基类所造成的歧义现象 虚基类用法 派生类继承基类时,加上一个virtual关键词则为虚拟基类继承. 在上图程序运行中,我们发现class bass的构造 ...
- java J2EE与DiscuzX3.2的UCenter实现单点登录
最近笔者在实现Java项目对discuz的整合.在此过程中,查了很多这方面的资料,发现网上并没有说得比较全面的文章.笔者博取众长以及自己在此过程中遇到的问题,写下来供大家参考,希望大家可以在这过程中少 ...
- redis python-redis 安装详细步骤
安装redis 把redis安装到 /opt/redis-2.8目录中 wget http://download.redis.io/releases/redis-2.8.1.tar.gz tar -z ...