The fewest coins
Description
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 has es 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 C1 coins 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 1: two space-separated integers: N and T.
Line 2: n space-separated integers, respectively V1, V2,..., vn coins (V1,... VN)
Line 3: n space-separated integers, respectively C1, C2,..., 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
Question:
FJ buys things. The value of things is T. He and the seller both have n kinds of gold coins. fj wants to minimize the change of gold coins when the transaction is completed.
Calculate the minimum change quantity of gold coins. FJ has a limited number of gold coins and an unlimited number of profitors.
Ideas:
For a backpack, the limited quantity of each gold coin of Fj can be seen as a problem of multiple backpacks, and the unlimited number of gold coins of a profiteer can be seen as a complete backpack problem.
Set F1 [I] to the minimum number of gold coins when the payment for FJ is I, and set F2 [I] to the minimum number of gold coins when the profiteer finds the money as I.
Then F1 [I + T] + F2 [I] is the minimum number of gold coins required.(F1 uses multiple Backpacks for solving, F2 uses a full backpack for solving)
PS: the backpack here is the minimum value, and it must be filled with exactly. Therefore, when initializing an arrayF [0] = 0, F [1-maxn] = int_max;(I haven't done a backpack for a long time. I subconsciously set f [1] to 0. When T = 1, I always output 0. I checked it for a long time ...)
Code:
1 # include <string> 2 # include <iostream> 3 # include <stdio. h> 4 # include <cstring> 5 # include <limits. h> 6 # define maxn 1000000 7 # define INF 9999999 // backpack 9 that is directly copied by the backpack. Because there are two arrays, an array parameter 8 using namespace STD is added; 9 int N, V, C [maxn + 10], a [maxn + 10], W = 1, F1 [maxn + 10], F2 [maxn + 10]; 10 int min (int A, int B) 11 {12 Return A> B? B: A; 13} 14 void zeroonepack (INT cost, int weight, int f []) // 01 backpack 15 {16 for (INT v = V; V> = cost; V --) 17 F [v] = min (F [v], F [V-cost] + weight); 18} 19 void completepack (INT cost, int weight, int f []) // full backpack 20 {21 for (INT v = cost; v <= V; V ++) 22 f [v] = min (F [v], F [V-cost] + weight); 23} 24 void multiplepack (INT cost, int weight, int amount, int f []) // multiple backpacks 25 {26 if (Cost * Amount> = V) 27 {28 completepack (cost, weight, f); 29 return; 30} 31 int K = 1; 32 While (k <amount) 33 {34 zeroonepack (K * cost, K * weight, f); 35 amount = amount-K; 36 K * = 2; 37} 38 zeroonepack (Amount * cost, amount * weight, f); 39} 40 void Init (int m, int f []) 41 {42 F [0] = 0; // ensure that the backpack is full. For details, see section 43 of "backpack 9" for (INT I = 1; I <= m; I ++) // to obtain the minimum value, assign the initial value to infinity (int_max may cause integer overflow) 44 f [I] = inf; 45} 46 int main () 47 {48 While (CIN> N> V) 49 {50 51 int v2 = V; 52 int max = 0; 53 for (INT I = 1; I <= N; I ++) {54 CIN> C [I]; 55 if (C [I]> MAX) max = C [I];} 56 for (INT I = 1; I <= N; I ++) 57 CIN> A [I]; 58 V = max * MAX + V2 + 10; // v is much larger than T, so 59 Init (v, F1 ); 60 Init (v, F2); 61 for (INT I = 1; I <= N; I ++) 62 multiplepack (C [I], 1, a [I], f1); 63 for (INT I = 1; I <= N; I ++) 64 completepack (C [I], 1, F2); 65 int ans = inf; 66 for (INT I = 0; I <= V-V2; I ++) 67 If (F1 [I + V2]! = Inf & F2 [I]! = Inf) ans = min (ANS, F1 [I + V2] + F2 [I]); 68 if (ANS! = Inf) printf ("% d \ n", ANS); // ans = inf indicates that the data has not changed, 69 else printf ("-1 \ n"); 70} 71 return 0; 72}