HDOJ-三部曲-多重背包-1014-Cash Machine
通过这道题我基本了解了利用二进制对多重背包问题进行优化的思想。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24191 | Accepted: 8466 |
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<cstring>
using namespace std;
int main()
{
int cash,n;
while(cin>>cash>>n)
{
int i,j,k=0,num[11],den[11],sc[10000];
int *dp=new int[100001];
memset(dp,0,(cash+1)*sizeof(int));
for(i=1;i<=n;i++)
{
cin>>num[i]>>den[i];
int t=1,amount=num[i];
while(amount>0) //用二进制思想进行优化,化为01背包问题
{
if(amount>=t)
{
sc[k++]=t*den[i];
amount-=t;
t*=2;
}
else
t=1;
}
}
/* for(i=0;i<k;i++)
cout<<sc[i]<<' ';
cout<<endl;*/
for(i=0 ;i<k;i++)
for(j=cash;j>=sc[i];j--)
dp[j]=max(dp[j],dp[j-sc[i]]+sc[i]);
cout<<dp[cash]<<endl;
delete dp;
}
}
HDOJ-三部曲-多重背包-1014-Cash Machine的更多相关文章
- hdoj 2191(多重背包)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/ ...
- hdu 2191 (多重背包+二进制优化)
Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品, ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- Cash Machine(多重背包二进制转换)
个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...
- 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 ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...
随机推荐
- python 使用*args 和**kwargs
def fun_var_args(farg, *args): print "arg:", farg for value in args: print "another a ...
- python中urllib和urllib2的简单用法
import urllib #引入urllib模块,这里用urllib2也可以 fpage = urllib.urlopen( url ) #打开网页:例如url=‘http://www.xxx.co ...
- MVC之URL路由
注册路由规则集合 一个 Web 应用具有一个全局的路由表,该路由表通过 System. Web.Routing.RouteTable的静态只读属性 Routes 表示,该属性返回一个类型为 Syste ...
- prepareStatement和Statement的区别
1:创建时的区别: Statement stm=con.createStatement(); PreparedStatement pstm=con.prepareStatement(sql ...
- ASP.NET Web API中的依赖注入
什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...
- MVP Community Camp 社区大课堂
MVP Community Camp 社区大课堂 微软技术社区大课堂开课啦!!!#MVPComCamp# 全中国微软最有价值专家MVP 在 3月21日周五全天齐聚北京国际会议中心为您呈 ...
- iBatisSQL中prepend的问题
是前向声明还是后向声明? 官方文档那个给出:“the overridable SQL part that will be prepended to the statement”.可见是前向声明. -- ...
- 用户上网的基本流程图与DNS解析原理
1.用户上网发送请求,首先确认本地的hosts中是否含有域名,有则进行ip访问,如果没有呢?看本机的display缓存中有没有访问网站的ip,有就直接去访问 那么如果本地的hosts和缓存都没有呢?这 ...
- superobject中 JavaToDelphiDateTime的使用
procedure TForm1.FormCreate(Sender: TObject); var n: TDateTime; i64: Int64; s: Integer; begin Memo1. ...
- redis学习(一)
一.redis简介 Redis是基于内存.可持久化的日志型.key-value高性能存储系统.关键字(Keys)是用来标识数据块.值(Values)是关联于关键字的实际值,可以是任何东西.有时候你会存 ...