POJ 1787 Charlie's Change

多重背包 可行性+路径记录

题意是说你要用很多其它的零钱去买咖啡。最后输出你分别要用的 1,5 ,10 。25 的钱的数量。

多重背包二进制分解。然后记录下 这个状态。最后逆向推就可以。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int dp[10001];
int cot[4];
int value[]={1,5,10,25};
int n;
struct lx
{
    int va,co;
}v[10001];
int zero(int cost,int i,int co)
{
    cost*=co;
    for(int j=n;j>=cost;j--)

        if(dp[j-cost]+co>dp[j])
        {
            dp[j]=dp[j-cost]+co;
            v[j].va=i;
            v[j].co=co;
        }
}
int main()
{
    while(scanf("%d",&n))
    {
        for(int i=0;i<4;i++)
            scanf("%d",&cot[i]);
        if(n==0)return 0;
        for(int i=0;i<=n;i++)
            dp[i]=-100001;
        dp[0]=0;

        for(int i=0;i<4;i++)
        {
            if(cot[i]==0)continue;
            if(value[i]*cot[i]>=n)
            {
                for(int j=value[i];j<=n;j++)
                    if(dp[j-value[i]]+1>dp[j])
                    {
                        dp[j]=dp[j-value[i]]+1;
                        v[j].va=i;
                        v[j].co=1;
                    }
            }
            else
            {
                int k=1;
                int tmp=cot[i];
                while(k<tmp)
                {
                    zero(value[i],i,k);
                    tmp-=k;
                    k*=2;
                }
                zero(value[i],i,tmp);
            }
        }
        if(dp[n]<=0)puts("Charlie cannot buy coffee.");
        else
        {
//            for(int i=0;i<=n;i++)
//                printf("%d :  %d*%d\n",i,v[i].co,v[i].va);
            memset(cot,0,sizeof(cot));
            while(n)
            {
                int i=v[n].va;
                int co=v[n].co;
                cot[i]+=co;
//                printf("%d ==\n",co);
                n-=value[i]*co;
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",cot[0],cot[1],cot[2],cot[3]);
        }
    }
}

POJ 1787 Charlie's Change

时间: 2025-01-19 23:34:06

POJ 1787 Charlie&#39;s Change的相关文章

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 - 1787 (多重背包还原路径|| 完全背包)

POJ  1787  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

(多重背包+记录路径)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

poj 1787 背包+记录路径

http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4512   Accepted: 1425 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at co

POJ 3087 Shuffle&amp;#39;m Up(模拟)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7404   Accepted: 3421 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

POJ 2528 Mayor&amp;#39;s posters 离散化+线段树

题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键是离散化的方法,这个题我时隔半年才A掉,之前一直就TTT,我还以为是线段树写挂了. 当我觉得我自己的水平这样的水线段树已经基本写不挂的时候又写了这个题,竟然还是T. 后来我对照别人的代码,才发现是我的离散化写渣了. 以下附AC代码(79ms),这个离散化写的比較优雅.时间也非常快,以后就这么写了.

POJ 2262 Goldbach&amp;#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.假设存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的全部素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,假设存在素数a+素数b