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. C++基础知识---static const初始化成员变量

    为了限制常数的范围class中.你必须要做出成为class成员:而要确保这是丝毫不亚于有一个恒定的实体.你必须要做出成为static员: Class Gameplayer { Private: Sta ...

  2. AngularJs 基础(60分钟入门) (转)

    AngularJs是一个不错的用于开发SPA应用(单页Web应用)的框架.单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.浏览器一开始会 ...

  3. 3DMax的OFusion插件使用问题

    使用OFusion将3D max场景导出为Ogre的Mesh是非经常常使用的方法.这里汇总一些问题方便查阅. 1.OFusion获取: http://download.csdn.net/detail/ ...

  4. Lichee(两) 在sun4i_crane该平台下编译

    让我们先来回顾一下编译命令 $ cd workdir/lichee $ ./build.sh -p sun4i_crane -k 3.0  lichee文件夹下的build.sh #!/bin/bas ...

  5. sql server 常用语法

    --1 创建数据库 DROP DATABASE mydb1 CREATE DATABASE mydb1 ON ( NAME ='mydb1',FILENAME='D:\mydb1.mdf') LOG ...

  6. 探索Oracle数据库升级6 11.2.0.4.3 Upgrade12c(12.1.0.1)

    探索Oracle数据库升级6 11.2.0.4.3 Upgrade12c(12.1.0.1) 一.前言:       Oracle 12c公布距今已经一年有余了,其最大亮点是一个能够插拔的数据库(PD ...

  7. C++ Primer笔记10_运算符重载_赋值运算符_进入/输出操作符

    1.颂值运营商 首先来福值运算符引入后面要说的运算符重载.上一节说了构造函数.拷贝构造函数:一个类要想进行更好的控制.须要定义自己的构造函数.拷贝构造函数.析构函数.当然,还有赋值运算符.常说的三大函 ...

  8. Thinkpad X200 屏幕备案

    妈妈蛋,屏幕废物前几天(闪屏->暗->变暗),因此,它只能监视房外 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk2NTg5MA= ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  10. 上传App时遇IDFA错误问题

    今天上传App时遇到下图1的情况,很纳闷,又是苹果新规. 通常是第三方的库引起啦,马上想到百度统计了,打开SDK下载页面看看简单介绍,里面有讲到这个问题了. 图2就是这次改动的原因. 更新SDK,之前 ...