Cash Machine
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24213 Accepted: 8476

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

Southeastern Europe 2002

都没用多重背包,随便写了个DP+二进制优化。。。。
 
 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

bool money[110000];
int num[1100],coin[1100];
int cash,n;

int main()
{
    while(scanf("%d",&cash)!=EOF)
    {
        scanf("%d",&n);
        memset(money,false,sizeof(money));
        money[0]=true;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&num,&coin);
            if(money[cash]) continue;
            for(int j=1;j<=num;j<<=1)
            {
                int val=j*coin;
                for(int k=cash;k>=0;k--)
                {
                    if(money[k]) if(k+val<=cash) money[k+val]=true;
                }
                num-=j;
            }
            if(num)
            {
                int val=num*coin;
                for(int k=cash;k>=0;k--)
                {
                    if(money[k]) if(k+val<=cash) money[k+val]=true;
                }
            }
        }
        int ans=0;
        for(int i=cash;i>=0;i--)
        {
            if(money)
            {
                ans=i; break;
            }
        }
        printf("%d\n",ans);
    }

return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 1276 Cash Machine的更多相关文章

  1. Poj 1276 Cash Machine 多重背包

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

  2. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  3. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  4. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  5. 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

    转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...

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

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

  7. [poj 1276] Cash Machine 多重背包及优化

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

  8. POJ 1276 Cash Machine(完全背包模板题)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44409   Accepted: 16184 Description A B ...

  9. POJ 1276 Cash Machine 【DP】

    多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...

随机推荐

  1. VmWare Workstation 10 安装 Ubuntu 14.04 问题解决

    Ubuntu安装过程很顺利,安装完成后还是有小问题存在   问题1:无法联网,PING可以通,网址无法解析 原因:默认DNS设置不正确 解决:设置DNS地址为8.8.8.8,问题解决   问题2:vm ...

  2. CMD命令下对文件夹进行权限处理 转

    保证自己的磁盘分区格式是NTFS.FAT32是不行的. 一.Cacls.exe命令的使用 这是一个在Windows 2000/XP/Server 2003操作系统下都可以使用的命令,作用是显示或者修改 ...

  3. php上传图片---初级版

    没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能. 图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性. 下面粘上代码: <? ...

  4. [poj3046]Ant Counting(母函数)

    题意: S<=x1+x2+...+xT<=B 0<=x1<=N1 0<=x2<=N2 ... 0<=xT<=NT 求这个不等式方程组的解的个数. 分析: ...

  5. DOM(十)使用DOM设置单选按钮、复选框、下拉菜单

    1.设置单选按钮 单选按钮在表单中即<input type="radio" />它是一组供用户选择的对象,但每次只能选一个.每一个都有checked属性,当一项选择为t ...

  6. 练习一_使用Git进行代码管理的心得

    2015年9月19日,第一次软工实践课.助教给我们介绍了git,GitHub.显而易见,我并没有听懂.所以整个上午都在找教程,一个字一个字对着敲,然后敲着敲着就出错,回宿舍,继续敲,也是一样的... ...

  7. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  8. 用 Docker 快速配置前端开发环境

    来源于:http://dockone.io/article/1714 今天是你入职第一天. 你起了个大早,洗漱干净带着材料去入职. 签了合同,领了机器,坐到工位,泡一杯袋装红茶,按下开机键,输入密码, ...

  9. [转]java基础学习总结——equals方法

    一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 1 package cn.galc.test; 2 3 public class TestEquals { 4 public s ...

  10. .net架构设计读书笔记--第三章 第10节 命令职责分离(CQRS)简介(Introducing CQRS)

    一.分离查询命令 Separating commands from queries     早期的面向DDD设计方法的难点是如何设计一个类,这个类要包含域的方方面面.通常来说,任务软件系统方法调用可以 ...