POJ1787——背包DP(特定状态+回溯)——Charlie's Change

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 2
16 0 0 0 1
0 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零钱的个数,问你是否能拆开,并求出最多的分成的零钱的情况第一次发现完全背包也能解决这种有限制的问题,一直以为只能在多重背包里面才能,原来用个if条件判断当前的num是否超过了t[i]就行1.完全背包做法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int v[5] = {0,1,5,10,25};
int dp[10010],ans[10010],num[10010],path[10010],t[5];
int p;
int main()
{
    while(~scanf("%d",&p)){
        for(int i = 1; i <= 4;  i++)
            scanf("%d",&t[i]);
        if((p+t[1]+t[2]+t[3]+t[4]) == 0) break;
        memset(dp,0,sizeof(dp));
        memset(ans,0,sizeof(ans));
        memset(path,0,sizeof(path));
        dp[0] = 1;
        for(int i = 1; i <= 4; i++){
            memset(num,0,sizeof(num));
            for(int j = v[i]; j <= p; j++){
                if(dp[j-v[i]] && dp[j-v[i]] + 1 > dp[j] && num[j-v[i]] < t[i]){
                    dp[j] = dp[j-v[i]] + 1;//使得后面每一个状态都是从前面一个得到,并且满足两个条件1:用去一个后的数目要比没用去的多2:用去的硬币数目不超过硬币本身
                    num[j] = num[j-v[i]] + 1;
                    path[j] = j - v[i];//难想到。。用来记录路径
                }
            }
        }
        int i = p;
        if(dp[p]>0){
            while(i!=0){
                ans[i-path[i]]++;//i-path[i] = i - ( i - v[i]) = v[i]
                i = path[i];//path[i]表示有i钱的时候用了一种硬币之后的钱的数目
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[1],ans[5],ans[10],ans[25]);
        }
        else printf("Charlie cannot buy coffee.\n");
    }
    return 0;
}

 2.

 

POJ1787——背包DP(特定状态+回溯)——Charlie's Change

时间: 2024-12-12 16:12:01

POJ1787——背包DP(特定状态+回溯)——Charlie's Change的相关文章

[bzoj1775][Usaco2009 Dec]Vidgame 电视游戏问题_背包dp

1775: [Usaco2009 Dec]Vidgame 电视游戏问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1775 题解: 发现是个$zz$分组背包. 但是,正常的分组背包是,完全背包+01背包,在这里根本行不通因为数据范围. 故此我们考虑背包$dp$. 状态:$f_{(i,j)}$表示前$i$组,$i$选,花费$j$的最大价值:$g_{(i,j)}$表示$i$不选. 因为空间开的下,所以我们可以把$i$也放进去. 不然$

POJ 1787 Charlie&#39;s Change

POJ 1787 Charlie's Change 背包系列 + 路程记录 乍一看.多重背包. 再乍.转化01背包?nonono,每样物品的数量达到了10^4,肯定会T. 再乍.看不出来了.去看大牛的题解吧. 哦——转化成完全背包. 怎么转?一般的完全背包是不限个数的.现在我们每一次枚举物品时记录一下取了多少个然后限制一下不要超过个数就好了. 恩.. 路程记录的话完全背包加一个path记录父亲..然后多重背包加一个num数组在dp的时候直接算一算..具体看代码..完全背包的那版路径记录感觉十分巧

真正的骗子(并查集+dp+dp状态回溯)

[//]: # (推荐题解模板,请替换blablabla等内容 ^^) ### 题目描述 一个岛上存在着两种居民,一种是天神,一种是恶魔. 天神永远都不会说假话,而恶魔永远都不会说真话. 岛上的每一个成员都有一个整数编号(类似于身份证号,用以区分每个成员). 现在你拥有n次提问的机会,但是问题的内容只能是向其中一个居民询问另一个居民是否是天神,请你根据收集的回答判断各个居民的身份. 输入格式 输入包含多组测试用例. 每组测试用例的第一行包含三个非负整数n,p1,p2p1,p2,其中n是你可以提问

poj1787 Charlie&#39;s Change

思路: 完全背包,记录路径. 实现: 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 int v[4] = {1, 5, 10, 25}; 5 int c[4], m; 6 int dp[10005], used[10005], path[10005]; 7 int main() 8 { 9 while (cin >> m >> c[0] >>

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 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

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 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

Charlie&#39;s Change POJ - 1787 (完全背包+记录路径)

完全背包输出路径:对于每一次更新记录一下路径:注意钱币个数: dp[i][0]代表在空间为i时需要多少枚钱币 dp[i][1]用来记录路径 cheek[j]用来记录在j时用了多少i枚钱币 思路在代码中: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define mem(a,b) memset(a,b,sizeof

Charlie&#39;s Change(完全背包+路径记忆)

Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 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 motores

BZOJ 1042 硬币购物(完全背包+DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d3.d4个相应的硬币能够拼出多少种总和为s? 思路:(1)首先,用完全背包求出f[i]表示四种硬币的数量无限制拼出i的方案数. (2)接着我们来理解 x=f[s]-f[s-(d1+1)*c1]的含义:x表示c1硬币的数量不超过d1个而其他三种硬币的数量不限制拼成s的方案数.我们举着例子来说明, 假设