zoj 2156 - Charlie'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 <string.h>

#define INF -100001
#define min( a, b ) ((a)<(b)?(a):(b))

int t[ 5 ];
int F[ 2001 ][ 5 ];
int C[ 5 ] = {0,1,1,2,5};
int T[ 5 ][ 15 ];

int main()
{
    int P,Q;
    while ( scanf("%d",&P) != EOF ) {
        for ( int i = 1 ; i <= 4 ; ++ i )
            scanf("%d",&t[ i ]);

        if ( !P ) break;
        Q = P%5;
        P = P/5;
        if ( t[ 1 ] < Q ) {
            printf("Charlie cannot buy coffee.\n");
            continue;
        }t[ 1 ] -= Q;t[ 1 ] /= 5;

        memset( F, 0, sizeof( F ) );
        for ( int i = 1 ; i <= P ; ++ i )
            F[ i ][ 0 ] = INF;
        F[ 0 ][ 0 ] = F[ 0 ][ 1 ] = Q;

        //二进制拆分
        for ( int i = 2 ; i <= 4 ; ++ i ) {
            int base = 1,numb = 0;
            while ( t[ i ] >= base ) {
                T[ i ][ ++ numb ] = base;
                t[ i ] -= base;
                base <<= 1;
            }
            if ( t[ i ] ) T[ i ][ ++ numb ] = t[ i ];
            T[ i ][ 0 ] = numb;
        }

        for ( int i = 2 ; i <= 4 ; ++ i ) {
            int e = T[ i ][ 0 ];
            for ( int j = 1 ; j <= e ; ++ j ) {
                int v = T[ i ][ j ];
                int u = v*C[ i ];
                for ( int k = P ; k >= u ; -- k )
                    if ( F[ k-u ][ 0 ] >= 0 && F[ k ][ 0 ] < F[ k-u ][ 0 ]+v ) {
                        for ( int l = 0 ; l <= 4 ; ++ l )
                            F[ k ][ l ] = F[ k-u ][ l ];
                        F[ k ][ 0 ] += v;
                        F[ k ][ i ] += v;
                    }
            }
        }

        //处理 cents 的
        for ( int i = t[ 1 ] ; i >= 0 ; -- i )
            if ( F[ P-i ][ 0 ] >= 0 ) {
                int s = t[ 1 ];
                F[ P-i ][ 0 ] += i*5;
                F[ P-i ][ 1 ] += i*5;
                s -= i;
                for ( int j = 4 ; j > 1 ; -- j ) {
                    int v = min( s/C[ j ], F[ P-i ][ j ] );
                    F[ P-i ][ j ] -= v;
                    F[ P-i ][ 1 ] += v*C[ j ];
                     F[ P-i ][ 0 ] += v*(C[ j ]-1);
                     s -= v*C[ j ];
                }
            }
        int max = INF,spa = P;
        for ( int i = 0 ; i <= t[ 1 ] ; ++ i )
            if ( max < F[ P-i ][ 0 ] ) {
                max = F[ P-i ][ 0 ];
                spa = P-i;
            }

        if ( F[ spa ][ 0 ] <= 0 )
            printf("Charlie cannot buy coffee.\n");
        else
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",
                F[ spa ][ 1 ],F[ spa ][ 2 ],F[ spa ][ 3 ],F[ spa ][ 4 ]);

    }
    return 0;
}

zoj 2156 - Charlie's Change

时间: 2024-10-02 03:57:03

zoj 2156 - Charlie's Change的相关文章

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的时候直接算一算..具体看代码..完全背包的那版路径记录感觉十分巧

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

[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

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] >>

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