The Fewest Coins
Posted 山杉三
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 V1, V2, ..., VN(1 ≤ Vi ≤ 120). Farmer John is carrying C1coins of value V1, C2 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 2: N space-separated integers, respectivelyV 1, V 2, ..., VN coins ( V 1, ... VN)
Line 3: N space-separated integers, respectivelyC 1, C 2, ..., CN
Output
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
Hint
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 }
以上是关于The Fewest Coins的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3260 The Fewest Coins(多重背包+全然背包)
BZOJ 1716 [Usaco2006 Dec]The Fewest Coins 找零钱
洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)