Cash Machine
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27804   Accepted: 9915

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each
denomination Dk the machine has a supply of nk bills. For example, 



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

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 



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

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

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

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact
amount of requested cash. 



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

给出n种货币,每种的价值和数量。问可得到最接近case的是多少?
多重背包问题,使用二进制转化为01背包。

从m转化为 12,4,2^(k-1) , m-2^k+1 ; k是由m-2^k+1>0的最大整数得到
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[110000] ;
int m[20] , a[20] ;
int main()
{
int c , n , i , j , l , k , mm ;
while(scanf("%d %d", &c, &n)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0] = 1 ;
for(i = 0 ; i < n ; i++)
scanf("%d %d", &m[i], &a[i]) ;
for(i = 0 ; i < n ; i++)
{
k = 0 ;
while( (1<<k) <= m[i] )
k++ ;
k-- ;
if(k == -1) continue ;
mm = (m[i] - (1<<k) +1 ) * a[i] ;
for(j = c ; j-mm >= 0 ; j--)
dp[j] += dp[j-mm] ;
for(l = 0 ; l < k ; l++)
{
mm = (1<<l)*a[i] ;
for(j = c ; j-mm >= 0 ; j--)
dp[j] += dp[j-mm] ;
}
}
for(i = c ; i >= 0 ; i--)
if( dp[i] )
break;
printf("%d\n", i);
}
return 0;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

poj1276--Cash Machine(多背包被判刑了)的更多相关文章

  1. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

  2. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  3. POJ1276:Cash Machine(多重背包)

    题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  6. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  7. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  8. POJ1276 Cash Machine

    Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Description A Bank pla ...

  9. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

随机推荐

  1. java -D參数简化增加多个jar【简化设置classpath】

    1.-D<name>=<value> set a system property  设置系统属性. java命令引入jar时能够-cp參数,但时-cp不能用通配符(多个jar时 ...

  2. Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式

    Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式 找到eclipse安装文件夹以下的plugins文件夹,搜索 org.eclipse.text ,找到一个jar ...

  3. sleep和wait的区别

    sleep指线程被调用时,占着CPU不工作,形象地说明为“占着CPU睡觉”,此时,系统的CPU部分资源被占用,其他线程无法进入,会增加时间限制.wait指线程处于进入等待状态,形象地说明为“等待使用C ...

  4. 使用Nexus搭建企业maven仓库(二)

    先阅读<使用Nexus搭建企业maven仓库(一)> http://blog.csdn.net/ouyida3/article/details/40747545 1.官网眼下最新的版本号是 ...

  5. DotNet基础

    DotNet基础 URL特殊字符转义 摘要: URL中一些字符的特殊含义,基本编码规则如下: 1.空格换成加号(+) 2.正斜杠(/)分隔目录和子目录 3.问号(?)分隔URL和查询 4.百分号(%) ...

  6. Learning React Native笔记

    React Native作为一个新事物,相关的资料还不多 官方的文档比较简单,缺少一些系统的例子 在对React Native的应用中,迫切的想学习一些别人的最佳实践.所以想通过看书系统的学习下 之前 ...

  7. 推荐一套.NET文档处理组件Spire.Office

    原文:推荐一套.NET文档处理组件Spire.Office 以前的项目中用到一点Word简单处理的功能(文字替换和转PDF格式),当时使用的是一套COM组件,必须在服务器上安装office环境.最近考 ...

  8. thinkphp框架相关研究(一)

    小编最近开始正式研究thinkphp框架,在此写下研究的整个历程,从最最基本的搭建网站开始,一步步记录.希望对大家有所帮助. 1.菜鸟从下载框架到建站 参考网址:http://blog.csdn.ne ...

  9. Redis11种Web应用场景

    Redis的一个非常大优点就是能够不用整个转入到这个数据库,而是能够沿用之前的MySQL等数据库,而仅在一些特定的应用场景通过Redis的特性提高效率.本文列出了11个这种Web应用场景,如显示最新的 ...

  10. loading加载中效果

    (function(){ try{ var ui={ loading:{ addCssStyle:function(text) { var head = document.getElementsByT ...