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

  1. 735 3 4 125 6 5 3 350
  2. 633 4 500 30 6 100 1 5 0 1
  3. 735 0
  4. 0 3 10 100 10 50 10 10

Sample Output

  1. 735
  2. 630
  3. 0
  4. 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的最大整数得到
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. int dp[110000] ;
  6. int m[20] , a[20] ;
  7. int main()
  8. {
  9. int c , n , i , j , l , k , mm ;
  10. while(scanf("%d %d", &c, &n)!=EOF)
  11. {
  12. memset(dp,0,sizeof(dp));
  13. dp[0] = 1 ;
  14. for(i = 0 ; i < n ; i++)
  15. scanf("%d %d", &m[i], &a[i]) ;
  16. for(i = 0 ; i < n ; i++)
  17. {
  18. k = 0 ;
  19. while( (1<<k) <= m[i] )
  20. k++ ;
  21. k-- ;
  22. if(k == -1) continue ;
  23. mm = (m[i] - (1<<k) +1 ) * a[i] ;
  24. for(j = c ; j-mm >= 0 ; j--)
  25. dp[j] += dp[j-mm] ;
  26. for(l = 0 ; l < k ; l++)
  27. {
  28. mm = (1<<l)*a[i] ;
  29. for(j = c ; j-mm >= 0 ; j--)
  30. dp[j] += dp[j-mm] ;
  31. }
  32. }
  33. for(i = c ; i >= 0 ; i--)
  34. if( dp[i] )
  35. break;
  36. printf("%d\n", i);
  37. }
  38. return 0;
  39. }

版权声明:转载请注明出处: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. 向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题

    原文:向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题 GridView的一个模板列中的内容是按钮,需要实现以下的效果: GridView分页显示数据,点击编辑按钮(模 ...

  2. hdu4352(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 题意:求区间L到R之间的数A满足A的的数位的最长递增序列的长度为K的数的个数. 分析:数位dp, ...

  3. hdu2089(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间[a,b]内不含有62或4的数的个数. 分析:数位dp,dp[pos][0]表示到第 ...

  4. 新版SDK自己主动加入PlaceholderFragment的思考

    自从Android SDK更新到22.6.3,发现新建Activity的时候,会自己主动生成一个Fragment.这个Fragment是activity的静态内部类.同一时候生成了一个xml叫frag ...

  5. IOS 与ANDROID框架及应用开发模式对照一

    IOS 和ANDROID操作系统都是眼下流行的移动操作系统,被移动终端和智能设备大量採用,两者都採用了先进的软件技术进行设计,为了方便应用开发两者都採用了先进的设计模式. 两者在框架设计上都採用了什么 ...

  6. start_kernel——boot_init_stack_canary

    /* * Initialize the stackprotector canary value. * * NOTE: this must only be called from functions t ...

  7. 利用 C++ 单向链表实现队列

    利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取. #pragma once #include "stdio.h ...

  8. uvaLive5713 次小生成树

    uvaLive5713 修建道路使得n个点任意两点之间都可以连通,每个点有都有一定的人口,现在可以免费修一条道路, A是免费修的道路两端结点的人口之和, B的其它不是免费修道路的长度的总和 要求的是A ...

  9. hdu - 4975 - A simple Gaussian elimination problem.(最大流量)

    意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...

  10. poj 2586 Y2K Accounting Bug (贪心)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8678   Accepted: 428 ...