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种面 ...
随机推荐
- c# BackGroundWorker 多线程操作的小例子
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...
- nyoj-----284坦克大战(带权值的图搜索)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" ...
- SQL SERVER 2008安装错误(is not a valid login or you do have permission)
在网上搜索查找问题: 原因:[计算机名] 与[账号名称]名称一致. 解决方案: 修改计算机名 桌面 -> 我的电脑 -> 右鍵点击属性 -> 选择计算机名选项卡 -> 更改 - ...
- CSS3 Media Queries
Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel= ...
- js在前端获取在本地上传图片的尺寸
var MyTest = document.getElementById("upload_img").files[0];var reader = new FileReader(); ...
- NetworkComms框架介绍 序列化并发送对象
NetworkComms网络通信框架序言 英文原文:http://www.networkcomms.net/custom-objects/ NetworkComms.Net网络库,支持发送自定义类,并 ...
- Eclipse中SVN的安装步骤(两种)和使用方法 [转]
一.给Eclipse安装SVN,最常见的有两种方式:手动方式和使用安装向导方式.具体步骤如下: 方式一:手动安装 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigri ...
- centos 卸载软件·
centos下完全卸载php 1显示相关软件的列表 rpm -qa | grep i(可以不加) php 2 卸载即可 rpm -e 软件名 --nodeps centos下完全卸载mysql 1显 ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- HDU 1358 简单kmp
题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...