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. 20145222黄亚奇《Java程序设计》第2周学习总结

    教材学习内容总结 类型: 整数:short(占2字节).int(4).long(8). 浮点数:float(4).double(8) 字符:char(2) 布尔:boolean类型表示true与fal ...

  2. Scrum敏捷精要

    本文抽取Scrum中的一些重要思想和概念,对Scrum敏捷执行的主题流程进行精要的介绍. 一.基本思想 个体和互动   高于   流程和工具 工作的软件   高于   详尽的文档 客户合作      ...

  3. Logparser 的用法

    Logparser是一款非常强大的日志分析软件,可以帮助你详细的分析网站日志.是所有数据分析和网站优化人员都应该会的一个软件.Logparser是微软的一款软件完全免费的,大家可以在微软的官网上去下载 ...

  4. bt协议详解 DHT篇(上)

    bt协议详解 DHT篇(上) 最近开发了一个免费教程的网站,突然产生了仔细了解bt协议的想法,这篇文章是bt协议详解系列的第三篇,后续还会写一些关于搜索和索引的东西,都是在开发这个网站的过程中学习到的 ...

  5. Django1.8教程——安装Django

    本书介绍 你是不是对Django的学习感到迷茫?是不是对网上零星的教程感到绝望?是不是苦于没有可以迅速上手的实例而发愁?如果你同我一样有这些感受,那么<Django.By.Example> ...

  6. java之Cookie详解

    Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...

  7. 第十四章:Annotation(注释)

    一:注解 1.当成是一种修饰符吧,修饰类及类的所有成员. 代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取. 2.@Override:强制子类覆盖(重写)父类的方法. @Deprecated ...

  8. Cas_Java客户端登录相关过滤器的处理流程

    首先了解一下CAS登录原理: 1.CAS结构中一般包含CAS服务器(Cas验证服务器).应用服务器(程序所在服务器).客户端(web浏览器)三个部分. 2.客户端向应用服务器发出请求,由于未登录,会被 ...

  9. Pro Git 读书笔记

    一. 起步 1. 集中式版本控制缺点:中央服务器的单点故障. 分布式版本控制优点:客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来. 这么一来,任何一处协同工作用的服务器发生故障,事后 ...

  10. Java算法-归并排序

    归并排序采用的是递归来实现,属于“分而治之”,将目标数组从中间一分为二,之后分别对这两个数组进行排序,排序完毕之后再将排好序的两个数组“归并”到一起,归并排序最重要的也就是这个“归并”的过程,归并的过 ...