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. Session为空的一种原因

    在维护一份比较老的代码,想改为ajax调用,然后就添加了一个一般处理程序文件,也就是以.ashx结尾的文件,一切都正常,但发现session一直为空,很奇怪 基本的代码如下: public class ...

  2. Linux iptables简单配置

    #!/bin/sh#modprobe ipt_MASQUERADEmodprobe ip_conntrack_ftpmodprobe ip_nat_ftpiptables -Fiptables -t ...

  3. HDU 4454 - Stealing a Cake(三分)

    我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...

  4. Json与Java对象互转之Gson学习

    Json与Java对象互转之Gson学习 请尊重他人的劳动成果.转载请注明出处:Json与Java对象互转之Gson学习         我曾在<XML,Object,Json转换之浅析Xstr ...

  5. pcie inbound、outbound及EP、RC间的互相訪问

    Inbound:PCI域訪问存储器域 Outbound:存储器域訪问PCI域 RC訪问EP: RC存储器域->outbound->RC PCI域->EP PCI域->inbou ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  7. Oracle错误——ORA-03113:在通信信道文件的末尾 解决方案

    起源 今天跟往常一样,登陆PL/SQL,确登陆失败,出现一个错误"ORA-01034"和"ORA-27101"如图: 然后就就通过命令提示符去登陆Oracle, ...

  8. WPF 各种基础动画实现

    C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  9. IOS加强知识(1)理解力Objective-C

    一直想写一般Objective-C帖子,总是没时间.所以,我希望有一个巨大的知识更小.温馨提示小的变化.写一点点,每天.东西把他们的学习分享,好了废话不多. 1.一门动态的语言OC Object-C( ...

  10. linux 流量监控 ---iptraf的安装及使用

    一.安装iptraf 我用的是centos,切换到root用户,执行 yum install -y iptraf 二.使用 1.直接输入iptraf,进入软件,按任意键继续 2.我主要是第二项和第三项 ...