POJ--1787--Charlie's Change--背包变形

来源:互联网 发布:网络机顶盒怎么看电视 编辑:程序博客网 时间:2024/06/05 01:07

Charlie's Change
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 3154 Accepted: 907

Description

Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. 

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly. 

Input

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.

Output

For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".

Sample Input

12 5 3 1 216 0 0 0 10 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.Charlie cannot buy coffee.

题意:告诉你1、5、10、25单位金币的数量和一个金币值n,求所有用这些金币组成这个n的方案中使用金币最多的那个,分别输出每种硬币使用的数量

昨天我可是瞅了大半天都没瞅出啥把戏出来啊,写了好几次,都是半路中间让我纠结,所以去溜别个的博客采阴补阳去了,看到一个写法跟我一样,但是犀利了很多,比如最后查询,巧妙的采用了 i 与 i-1 之间的差值,哎,是我太笨

解析:多重背包,把金额作为容量,也就是dp的下标,dp[x]中装的是组成金额为 x 的使用金币数量的最大值,然后我用dis记录前驱点,比如dp[x]=dp[x-v]+m,则dis[x]=x-v,

然后重要的是每次作用的金币要在对应的dp[ ]数组中记录这个点所在路径上使用的数量


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main (void){    int n,m,i,j,k,l,num[4],val[4]={1,5,10,25};    int dp[11111],dis[11111],v[11111];    int x[33];    while(~scanf("%d",&n))    {        if(n==0)k=1;        else k=0;        for(i=0;i<4;i++)        {            scanf("%d",&num[i]);            if(num[i])k=0;        }        if(k)break;        memset(dp,-1,sizeof(dp));        dp[0]=0;        memset(dis,0,sizeof(dis));        dis[0]=-1;        for(i=0;i<4;i++)        {            memset(v,0,sizeof(v));            for(j=val[i];j<=n;j++)            if(dp[j-val[i]]>=0&&v[j-val[i]]<num[i]&&dp[j]<dp[j-val[i]]+1)            {                dp[j]=dp[j-val[i]]+1;//使用金币数量最大值                dis[j]=j-val[i];//记录前驱点,前驱点的金额加上这次装入的金币金额等于这次的金额                v[j]=v[j-val[i]]+1;//记录 i 号金币已经在这条路径上使用的数量            }        }        if(dp[n]>0)        {            memset(x,0,sizeof(x));            i=n;            while(dis[i]>=0)            {                x[i-dis[i]]++;//dis中:a+金币金额=b,所以金币金额=b-a                i=dis[i];//直接往记录的前驱点跳跃            }            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",x[val[0]],x[val[1]],x[val[2]],x[val[3]]);        }else        {            puts("Charlie cannot buy coffee.");        }    }    return 0;}

总结:我就记得01背包跟完全背包,然后脑子里面还有一个多重背包,我记得是数量从1到K一个一个去01背包的,这个方法很耗时,然后是有个二进制压缩的方法,使用2的0次方到n次方来进行完全背包,这个是一般多重背包通用的方案,这次不会写多重了,毕竟很久没动了,所以看了自己以前写的,结果。。尼玛,看懂了,却发现这个题在使用的时候要考虑情况,就是没有方案的时候不能叠加,哎,以后这做题不能停的,停一天可就要当一年的傻逼了


0 0