poj1787 Charlie'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] >> c[1] >> c[2] >> c[3], m || c[0] || c[1] || c[2] || c[3])
10     {
11         memset(used, 0, sizeof used);
12         memset(path, 0, sizeof path);
13         path[0] = -1;
14         for (int i = 0; i <= m; i++) dp[i] = -INF;
15         dp[0] = 0;
16         for (int i = 0; i < 4; i++)
17         {
18             memset(used, 0, sizeof used);
19             for (int j = v[i]; j <= m; j++)
20             {
21                 if (dp[j - v[i]] != -INF && dp[j - v[i]] + 1 > dp[j] && used[j - v[i]] < c[i])
22                 {
23                     dp[j] = dp[j - v[i]] + 1;
24                     used[j] = used[j - v[i]] + 1;
25                     path[j] = j - v[i];
26                 }
27             }
28         }
29         if (dp[m] <= -INF) { cout << "Charlie cannot buy coffee." << endl; }
30         else
31         {
32             int ans[26]; memset(ans, 0, sizeof ans);
33             while (path[m] != -1)
34             {
35                 ans[m - path[m]]++;
36                 m = path[m];
37             }
38             cout << "Throw in " << ans[v[0]] << " cents, " << ans[v[1]] << " nickels, " << ans[v[2]] << " dimes, and " << ans[v[3]] << " quarters." << endl;
39         }
40     }
41     return 0;
42 }

poj1787 Charlie's Change

时间: 2024-10-10 16:43:39

poj1787 Charlie's Change的相关文章

POJ1787——背包DP(特定状态+回溯)——Charlie&#39;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

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

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

POJ 1787 Charlie&#39;s Change

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

[POJ 1787]Charlie&#39;s Change (动态规划)

题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货币要用多少钱. 据说此题有完全背包的写法.. 我是按照多重背包写的,速度也不是很慢. 然后记录了下前驱. 刚开始全都写挫了..虽然现在也很挫.. 凑合着看吧 -- 1 #include <cstdio> 2 #include <algorithm> 3 #include <cst

POJ Charlie&#39;s Change 查理之转换(多重背包,微变形)

题意:给定身上的4种硬币,分别是1 ,5 ,10, 25面额各有多张,要求组成面额p的硬币尽可能多.输出组成p的4种硬币各自的数量. 思路:多重背包,300+ms.用01背包+二进制的方法.记录下所有的硬币的个数以及4种硬币分别的个数,注意初始化dp数组的不同之处. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define LL long long 5 using namespace

台州 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