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种面 ...
随机推荐
- 6/7 Sprint2 看板和燃尽图
- SDWebImage源码刨根问底
前言: SDWebImage是iOS中一款处理图片的框架, 使用它提供的方法, 一句话就能让UIImageView,自动去加载并显示网络图片,将图片缓存到内存或磁盘缓存,正好有阅读开源项目的计划,于是 ...
- SQL Server 一列或多列重复数据的查询,删除
业务需求 最近给公司做一个小工具,把某个数据库(数据源)的数据导进另一个数据(目标数据库).要求导入目标数据库的数据不能出现重复.但情况是数据源本身就有重复的数据.所以要先清除数据源数据. 于是就把关 ...
- Android SDK的安装以及环境变量的配置
想来试试app的开发,于是便需要在电脑上安装各种软件包,其中Android SDK就是其中一种,特地记录一下其安装过程: 1.下载地址http://baoku.360.cn/soft/show/app ...
- go——搭建Win7下的Go开发环境
1.首先需要下载下载go平台安装包 安装程序 下载地址:https://golang.org/dl/ (墙内下载地址http://www.golangtc.com/download),如果是您的系统是 ...
- Axis2 webservice入门--写个简单的webservice
上一篇介绍了webservice开发前的准备.下面开始写webservice.如果不了解axis2请看上一篇,如果是新手:建议一边看一边写代码,自己动手完成这个过程. 一.新建一个web项目 二.新建 ...
- 怎么设置 mysql 多主复制
更新 其实本文主要来自www.digitalocean.com ,但是我没有买他们家的 VPS 用来 demo 了.只是用vagrant 来模拟了. 介绍 说说关于通过两台 vps 来扩展 mysql ...
- myeclipse设置编码格式的4种情况
(1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...
- 使用WebView视图显示网页-----迷你浏览器
Android提供了WebView组件,表面上来看,这个组件与普通ImageView差不多,但实际上,这个组件的功能要强大得多,WebView组件本身就是一个浏览器实现,它的内核基于开源WebKit引 ...
- ASP.NET MVC的Ajax.ActionLink 的HttpMethod="Get" 一个重复请求的BUG
这段时间使用BootStrap+Asp.net Mvc5开发项目,Ajax.ActionLink遇到一个重复提交的BUG,代码如下: @model IList<WFModel.WF_Temp&g ...