POJ 1276 Cash Machine

时间:2022-03-31 13:34:21
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 -- 动态规划&lpar;背包问题&rpar;

    题目地址: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&lpar;多重背包的二进制优化&rpar;

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

  7. &lbrack;poj 1276&rsqb; 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. java 深入技术六(Map)

    Map 1.map概述 map.put(key,value)里面存放的是两个相关的数据,key=value键值对 Map集合中存放的是键值对(put(key,value)),用get(key)获取集合 ...

  2. dedecms文章标题是在哪个数据库表?要批量替换关键词

    一位小MM刚接触dedecms没多久还不熟悉后台的操作,她说改dedecms文章中的品牌名改到手酸,问ytkah是否有批量替换关键词的方法,教了她dedecms后台批量替换文章中的关键词方法,她高兴坏 ...

  3. Eclipse启动Tomcat访问不了首页

    Eclipse开发web项目与myEclipse不同: 启动服务器后访问 http:localhost:8080 找不到服务器 想要访问Tomcat首页只需修改Tomcat配置 进入Eclipse双击 ...

  4. Tomcat Server 原理

    构成: 1.server代表整个catalina serverlet容器 2.service:由一个或多个connector以及一个共享的engine处理引擎组成 3.connector 在指定端口上 ...

  5. python udp编程实例

    与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...

  6. PHP 接口 返回构造函数

    我们写接口的时候,需要返回json数据,并且里面有错误码,错误信息 还有要返回的数据,这里我构造了一个函数 这里是针对TP5来写的,自己可以根据自己的框架来修改 这样就可以在返回的时候直接用info函 ...

  7. Linux2&period;6内核--抢占

    [摘要]本文首先介绍非抢占式内核(Non-Preemptive Kernel)和可抢占式内核(Preemptive Kernel)的区别.接着分析Linux下有两种抢占:用户态抢占(User Pree ...

  8. Tornado的安装使用

    https://blog.csdn.net/a312024054/article/details/52207367 tornado原理: tornado的使用 import tornado.ioloo ...

  9. QT4编程过程中遇到的问题及解决办法

    1.QLineEdit显示内容的格式函数: QLineEdit *lineEditPassword = new QLineEdit: lineEditPassword -> setEchoMod ...

  10. mysql source 执行sql脚本,中文变量不显示问题或乱码问题

    执行脚本内容如下: SET @pre_version=2017080901; SET @cur_version=2017090401; SET @ver_desc = '测试脚本'; CALL pro ...