Cash Machine
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32971   Accepted: 11950

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

-----------------------------------------------------------------------------------------------
多重背包可行性问题
法1:O(NVlogC)
二进制拆分,w无意义,c和v不开数组也可以
//  poj1276
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=,V=1e5+;
int n,cash,f[V],c[N],w[N],v[N]; inline void zp(int v,int w){
for(int i=cash;i>=v;i--)
//f[i]=max(f[i],f[i-v]+w);
f[i]=f[i]|f[i-v];
}
inline void cp(int v,int w){
for(int i=v;i<=cash;i++)
// f[i]=max(f[i],f[i-v]+w);
f[i]=f[i]|f[i-v];
}
inline void mp(int v,int w,int c){
if(c*v>=cash){cp(v,w);return;}
int k=;
while(k<c){
zp(k*v,k*c);
c-=k;
k*=;
}
zp(v*c,w*c);
} int main(int argc, const char * argv[]) {
while(cin>>cash>>n){
f[]=;
memset(f,,sizeof(f)); f[]=;
for(int i=;i<=n;i++){
cin>>c[i]>>v[i];
mp(v[i],w[i],c[i]);
}
for(int i=cash;i>=;i--)if(f[i]>){cout<<i<<"\n";break;}
}
return ;
}

法2:O(NV) 然而却比法1慢...............

f[i][j]表示前i种物品装满容量为j的背包后还剩下几个i,不可行为-1  

转移先通过f[i-1][j]判断可不可行,再用可行的f[i][j]更新f[i][j+vi](其实就是一遍完全背包)

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=,V=1e5+;
int n,cash,f[N][V],c[N],v[N]; void mpAble(){
memset(f,-,sizeof(f));
f[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=cash;j++){
if(f[i-][j]>=) f[i][j]=c[i];
else f[i][j]=-;
}
for(int j=;j<=cash-v[i];j++)
if(f[i][j]>=)
f[i][j+v[i]]=max(f[i][j+v[i]],f[i][j]-);
}
}
int main(int argc, const char * argv[]) {
while(cin>>cash>>n){
for(int i=;i<=n;i++) cin>>c[i]>>v[i];
mpAble();
for(int i=cash;i>=;i--)if(f[n][i]>=){cout<<i<<"\n";break;}
}
return ;
}

POJ1276Cash Machine[多重背包可行性]的更多相关文章

  1. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  2. Poj 1276 Cash Machine 多重背包

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

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

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

  4. POJ 1276:Cash Machine 多重背包

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

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

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

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

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

  7. Cash Machine(多重背包)

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

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

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

  9. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

随机推荐

  1. CSS3中DIV水平垂直居中-2(3)

    用到CSS3中display的新属性. HTML <div class="parent"> </div> CSS html,body{ width: 100 ...

  2. 轻松掌握:JavaScript装饰者模式

    装饰者模式 在传统的面向对象语言中,给对象添加功能常常使用继承的方式,但继承的方式会带来问题:当父类改变时,他的所有子类都将随之改变. 当JavaScript脚本运行时,在一个对象中(或他的原型上)增 ...

  3. BT5 & Kali Linux 网卡选择

    [需要注意的几点]: 芯片类型 知否支持外接天线 网卡固件版本 支持的无线协议 网卡功率 BT5/Kali  RC3支持网卡: RTL8187, R8187 (Realtek) 功率高.兼容性好 Ra ...

  4. 转:jQuery.data

    原文地址:http://www.it165.net/pro/html/201404/11922.html 内存泄露 首先看看什么是内存泄露,这里直接拿来Aaron中的这部分来说明什么是内存泄露,内存泄 ...

  5. 使用Masonry搭建特殊布局时与xib的对比

    之前只有比较浅的接触过Masonry.项目中大多数的布局还是用xib中的AutoLayout与手码的frame计算相结合,相信也会有很多项目和我一样是这两种布局的组合.其实xib各方面用的感觉都挺好, ...

  6. runtime学习笔记

    获取属性objc_property_t * propertys = class_copyPropertyList(clazz, &outCount); 获取属性名NSString * key ...

  7. [Android]listview recycleview的复用问题

    最近解决了几个bug,是关于listview和recycle view中的复用问题的:   为了提高性能,我们使用了viewHolder来减少view的生成,从而提高滑动的性能:   要注意一个很隐蔽 ...

  8. HTML5设计网页动态条幅广告(Banner) 已经加上完整源代码

    横幅广告(Banner): 1.横幅广告是网络广告的常见形式,一般位于网页的醒目位置上:当用户单击这些横幅广告时,通常可以链接到相应的广告页面: 2.设计横幅广告时,要力求简单明了,能够体现出主要的中 ...

  9. 利用PHPMailer 来完成PHP的邮件发送

    翻起之前的代码看了一下,还是发表到这里,以后容易查找. 以下的两个文件在这里下载 http://download.csdn.net/detail/u013085496/9673828 也可以直接上gi ...

  10. 《Hey程序员 你适合加入创业公司吗?》再补充

    笔者经过多年的走访发现,不是所有优秀的程序员都能在创业公司如鱼得水.根据笔者的经验,具备下面几点优秀品质的程序员会更容易适应创业公司的环境. 1.娴熟的调试技巧可以说,程序员的大部分时间都花在调试程序 ...