The Fewest Coins

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1V2, ..., VN(1 ≤ Vi ≤ 120). Farmer John is carrying C1coins of value V1C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

Input

Line 1: Two space-separated integers: N and T
Line 2: N space-separated integers, respectivelyV 1V 2, ..., VN coins ( V 1, ... VN
Line 3: N space-separated integers, respectivelyC 1C 2, ..., CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.
 
题目意思: 给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候, 价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
解题思路:多重背包和完全背包混合使用
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include<math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int MAX = 40000;
 9 int N,M;
10 int a[MAX],num[MAX],dp1[MAX],dp2[MAX],dp3[MAX];
11 int main()
12 {
13     while(~scanf("%d %d",&N,&M))
14     {
15         for(int i=1;i<=N;i++)
16             scanf("%d",&a[i]);
17         for(int i =1;i<=N;i++)
18             scanf("%d",&num[i]);
19 
20         for(int i=1;i<=MAX;i++)
21             dp1[i]=dp2[i]=MAX*1000;
22         dp1[0]=dp2[0]=0;
23         for(int i=1;i<=N;i++)
24         {
25             memset(dp3,0,sizeof(dp3));
26             for(int j=a[i];j<=MAX;j++)
27             {
28                 if(dp1[j]>dp1[j-a[i]]+1&&dp3[j-a[i]]<num[i])
29                 {
30                     dp1[j] = dp1[j-a[i]]+1;
31                     dp3[j] =dp3[j-a[i]] +1;
32                 }
33                 dp2[j] = min(dp2[j],dp2[j-a[i]]+1);
34             }
35         }
36 
37         int minx=MAX*1000;
38         for(int i = M;i<=MAX;i++)
39         {
40             minx = min(minx,dp1[i]+dp2[i-M]);
41         }
42         if(minx>=MAX*1000)
43             printf("-1\n");
44         else
45             printf("%d\n",minx);
46 
47 
48     }
49 
50     return 0;
51 }



转载于:https://www.cnblogs.com/a2985812043/p/7375671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值