poj1276 Cash Machine

9 篇文章 0 订阅
Cash Machine
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 38344 Accepted: 13931

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

cash N n1 D1 n2 D2 ... nN DN 

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash. 

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

题意:给你cash的值 钱币的种数以及每种钱币的面额和张数

求你能得到的最接近cash的值M(M<=cash);



大佬的题解:

如果设每个物品的价值为w[i],体积(可理解为消耗的价值)为c[i]

那么必有 d[i] = w[i] = c[i]

 

如果把一个金额看为一种状态,那么一共有0~cash种状态

显然其中可能会发生的状态范围为min{w[i] | 1<=i<=n[i]} ~ Cash

那么可以建立一个状态数组dp[cash+1],

其中dp[j]记录的是“最接近状态j,且<=j”的状态值,即dp[j] <=j

J越接近dp[j],dp[j]的解越优,最理想是dp[j]=j

需要注意的是,dp[j]为了说明的是状态j可以发生,但不会理会dp[j]怎样发生

例如有3张1元,1张3元

那么我们既可以认为dp[3]=3是通过取3次1元得到,也可以认为dp[3]是通过取1次3元得到的,但无论怎样得到,dp[3]=3都会发生,

再需要注意的是,dp[j]的状态值会累积

再形象举例说明:例如有1张3元,cash=4

那么根据前面的说明,自然有dp[3]=dp[4]=3,就是说状态3、状态4都可以通过取1次3元发生,一旦发生,这个状态值3就会被保有在当前的状态dp[4],这其实是起到一个优化作用,后面详细解释

再接上例,当我们增加一个条件“有3张1元”,那么在已经被保存的前提“dp[4]=3”下,我们只需要通过取1次1元,就能得到比dp[4]=3的更优解dp[4]=4,但此时我们完全不用理会dp[4]=3是怎样来的,我们只需要知道dp[4]=3一定会出现就足够了

 

然后说说刚才提到的“优化问题”

利用01背包的知识,不难理解 状态方程 dp[j]=dp[ j-c[i] ]+w[i]

至于说方程是什么含义,看过01背包的大概也知道什么意思,没看的同学就别怪我不解释咯O(∩_∩)O

优化是因为状态值被记录了,就是说我们在取得下一个货币i之前,当前的状态为j-c[i]

一旦我们选取货币i,就会得到状态(j-c[i])+c[i],即状态j 。且状态j的状态值dp[j]会加上w[i]。 至于dp[j]原来值是多少,就无需理会,因为dp[j]的值是累积下来的,同样本次加上w[i]后,只要dp[j]值未到最优,它同样会成为下一次的累加值。

这样每次都直接调用前一次已获得的状态值,就可以节省一堆搜索的时间,这是DFS或BFS办不到的,也是动态规划的优点。

 

接下来说说计数器count[j]

在我的程序中,每更换一次面额,计数器会清零,这样做是为了 以面额i的价值w[i]为权,根据选取该面额的个数,对状态值dp[j]进行w[i]的整数倍分割,这样就能得到对于每组状态dp[w[i]*k]到dp[w[i]*(k+1)] (0<=k<=n[i])之间,在当前面额w[i]下的最优状态值。

例如有 3张3元

自然dp[0]=dp[1]=dp[2]=0,count[0]= count[1]= count[2]=0这是因为最小面额为3

不难得到dp[3]=dp[4]=dp[5]=3,count[3]= count[4]= count[5]=1这是因为面额3元不可分,这3个状态的最优值就是取1次3元

dp[6]=dp[7]=dp[8]=6  , count[6]= count[7]= count[8]=2这3个状态的最优值就是取2次3元

dp[9]=dp[10]=dp[11]=dp[…]=9 ,count[9]= count[10]= count[[11]= count[…]=3  最多只有3张3元,以后的的状态的最优值均为9



c[i] 第i种钱币所需的体积,w[i]第i种钱币的面值,这里c[i] = w[i];

我的理解:dp,从状态0到cash,用dp[j]保存目前所取的小于cash的最大值,那么

对于第i种钱币来说dp[j] = dp[j-c[i]]+w[i] 

因为c[i] = w[i],因此不用考虑会出现dp[j] > j 的情况

所以只需要对每种钱币都跑一次,每次取大值,注意控制一下每种钱币的数量就好;


ac代码

#include<iostream>
#include<cstdio>
#include<cstring>

int dp[100005];
int cnt[100005];
int N,cash;
int n[15];
int c[15];
int w[15];


int main()
{
    while(~scanf("%d %d",&cash,&N))
    {
        for(int i = 1; i <= N; i++)
        {
            scanf("%d %d",&n[i],&w[i]);
            c[i] = w[i];
        }
        memset(dp,0,sizeof(dp));

        for(int i = 1; i <= N; i++)
        {
            memset(cnt,0,sizeof(cnt));
            for(int j = w[i]; j <= cash; j++)
            {
                if(dp[j] < dp[j-c[i]]+w[i] && cnt[j-c[i]] < n[i])
                {
                    dp[j] = dp[j-c[i]] + w[i];
                    cnt[j] = cnt[j-c[i]] + 1;
                }
            }
        }

        printf("%d\n",dp[cash]);

    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值