POJ-1276 Cash Machine 动态规划

1.题干

Problem 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. The program input is from a text file. Each data set in the file 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. 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 InputSample OutputComment
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
735
630
0
0
735=1* 350+3* 125+2* 5
630=6* 100+1* 30 or 21* 30
No cash delivered
No cash delivered

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.

2.翻译及大致思路

1.翻译:

一家银行计划安装一台取款机,这种机器可以根据用户要求吐出适当金额的@现金,它用N种确定面额的钞票,而且Dk面额的有nk张,例如:

N=3,              n1=10,      D1=100,     n2=4,          D2=50,          n3=5,         D3=10
意思是这台机器有10         张100,           4              张50,            和5张             10块.

为了可以根据要求支付现款,取款机应该有一个程序来计算出最大金额,这个金额应该小于等于取款机根据上面数据可以提供的金额.

2.大致思路:

解题思路:
  本题有多组测试数据,每组数据占一行,包括现金cash, 票据种类n,之后跟随n组数据每组包括票据数量,票据金额。要求输出小于等于现金的票据最大金额。

  可以将本题转化为0 1背包问题,背包容量为现金数cash,背包内容物价值与占空间数都为票据面值。

  基本思路是将每个面值的票据拆分,由于票据数量较大,直接拆分运算时会超时,所以我们对拆分进行优化。

 

二进制优化:

  假设某一面值的票据有100张,我们并不需要将100张票据全部加入运算数组,我们将100拆分为数个小于100的数字,使这些数字可以构成1 ~ 100中任意一个数字。这里用到了一个数论的小知识:1,2,4 ~ 2^n 可以组成1 ~ 2^(n + 1) - 1之间的任意数,100便可分解为1,2,4,8,16,32,(取2的n次幂(二进制数),不能取到64,因为拆分100的话所有分解的数加起来不能超过100) 37(前面取到的1 ~ 32已经可以表示63以内所有的数了,那么再补上100 - 63 = 37这个数后就可以取到所有1 ~ 100的数了)。继续分析,假设这100张票据面值都为2,那我们就可用到我们分解出来的数字,根据这些数字我们把100张票分解1张,2张,4张,8张,16张,32张,37张这7组,这样我们便可以把100张面值为2的票据看成面值为2,4,8,16,32,64,74的7张票据。将这7张票据加入运算数组在运算时比起100张票据就要节约很多时间。

  至于为什么1,2,4,8,16,32,37可以代替100,这很简单,之前已经写过了新的数组可以表示1 ~ 100所有数组,那么在运算中,我们如果需要拿5张面值1,在这里就和拿一张面值1一张面值4有同样效果拿其他所有1 ~ 100的数皆是这个道理。

 

背包思路:

  动态规划,令dp[ j ]表示chsh为 j 时能得到的票据的最大金额。

  对于第 i 张票据,有用或不用两种方案。

  1、用第 i 块票据,问题转化为计算背包容量为 j - value[ i ] 在前i - 1张票据中取得最大金额问题dp[ j ]的值为前i - 1张票据中取得的最大金额 + 第 i 张票据的金额。

  2、不用第 i 张票据,问题转化为背包容量为 j 时在前i - 1张票据中取得最大金额问题,dp[ j ]的值为前i - 1张票据中取得的最大金额。

  动态转移方程:dp[ j ] = max(dp[ j ], dp[ j - value[ i ] ] + value[ i ])

  边界为用前0张票据,最大金额为0,枚举所有票据,每次从最大背包容量开始逆序枚举便可获得答案。

3.代码:

#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int a[1000];
        int t=0;
        for(int i=0;i<m;i++)
        {
            int s1,v1;
            scanf("%d%d",&s1,&v1);
            for(int j=1;s1>0;j=j*2)
            {
                if(s1>=j)
                {
                    s1=s1-j;
                    a[t]=j*v1;
                    t++;
                }
                else
                {
                    a[t]=s1*v1;
                    s1=0;
                    t++;
                }
            }
        }
        int b[100010]={0};
        int ma=0;
        for(int i=0;i<t;i++)
        {
            for(int j=n;j>=a[i];j--)
                b[j]=max(b[j],b[j-a[i]]+a[i]),ma=max(ma,b[j]);
        }
        printf("%d\n",ma);
    }
    return 0;
}

注释:本文章主要用于本人学习,归纳,总结,如有不足之处,请您指出来,飞常感谢!!!

借鉴博客:https://blog.csdn.net/dbt3498/article/details/101237128

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值