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. ZOJ 1610 间隔染色段树

    要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水   水可太暴力 段树: #include "stdio.h" #include ...

  2. poj2236(并查集)

    题目连接 题意:一张图上分布着n台坏了的电脑,并知道它们的坐标.两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连.给出操作“O x”表示修好x,给出操作“S x y”,请 ...

  3. [置顶] Android常用适配器控件

    Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...

  4. SE 2014年4月24日

    如图配置交换网络 由于网络规模较小,企业将网络划分为了接入层和核心层两层 核心层设备(Sw1 Sw2 Sw3)作为用户的网关设备,提供三层转发功能 接入层设备(SW4 SW5)连接用户,分别划分三vl ...

  5. Java利用httpasyncclient进行异步HTTP请求

    Java利用httpasyncclient进行异步HTTP请求 前段时间有个需求在springmvc mapping的url跳转前完成一个统计的业务.显然需要进行异步的处理,不然出错或者异常会影响到后 ...

  6. 项目中那些事|ListView中嵌套ListView问题

    要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...

  7. 自己定义 ViewGroup 支持无限循环翻页之三(响应回调事件)

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处,再次感谢 ################################ ...

  8. [Oracle] - 性能优化工具(4) - AWRDD

    AWRDD是用于比較两个AWR快照,从而获得不同一时候期的性能. 运行例如以下语句获得AWRDD: @?/rdbms/admin/awrddrpt.sql 2025 23 2月 2014 07:12 ...

  9. java性能缓慢

    虚拟帝国上面有很多营销软件是JAVA开发的!创业公司通常选择开源技术减少项目管理费用. 除了使用Java编程语言,创业公司也可以利用Java开发工具包的好处(JDK),Java运行时环境(JRE)和J ...

  10. pygame系列_游戏中的事件

    先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ...