In real life, we often encounter coin-seeking issues. For example, when sending a salary, financial personnel need to calculate the minimum number of coin-seeking, so that they can get the minimum number of coins from the bank and make sure they can pay for the coins. We should note that the RMB coin system is 0.5, 50, 20, 10, 5, 2, 1, 0.2, 0.1, 0.05, 0.02, 0.01, Yuan, using these coins, we can use greedy algorithms to find the minimum number of coins for any wage. However, unfortunately, we may not have such a good coin system. Therefore, we cannot use greedy algorithms to find the minimum number of coins. Even some coins cannot be used to change the total number of coins. For example, if the coin system is 40, 30, 25 yuan, then 37 Yuan won't be able to use these coins to change to zero; 95 yuan's minimum change to zero Coin number is 3. For example, if the coin system is 10, 7, 5, or 1 yuan, then 12 yuan uses the greedy method to get 3 coins, and the minimum number of coins is 2. Your task is: For any coin system and a coin number, please program to find the minimum number of coins; if you cannot use these coins to find the number, please give a way to find the zero, minimize the remaining money. Input data: 1st rows, which are N and T. 1 ≤ n ≤ 50 indicates the number of coins in the coin system, and 1 ≤ T ≤ 100000 indicates the total number of coins to be changed. 2nd behavior n positive integers not greater than 65535, which are the denominations of each coin in the coin system. Output Data: If t can be changed to zero by a coin in the coin system, please output the minimum number of coins to be changed. If t cannot be changed to zero by a coin in the coin system, output the minimum number of coins in the change plan with the lowest remaining amount of money. Example input file name: coin. In 4 12 10 7 5 1 output file name: coin. out 2
A simple full backpack, needless to say.
The O (Mn) algorithm uses the side effects of the local movement of the Zero-One backpack (that is, changes the reverse order of the parts to the forward order of the parts), as shown in the program.
Accode:
#include <cstdio>int f[100010], c[60], n, m;int min(int a, int b) {return a < b ? a : b; }int main(){ freopen("coin.in", "r", stdin); freopen("coin.out", "w", stdout); scanf("%d%d", &n, &m); for (int i = 1; i < n + 1; ++i) scanf("%d", c + i); for (int j = 1; j < m + 1; ++j) f[j] = 0x3fffff00; for (int i = 1; i < n + 1; ++i) for (int j = c[i]; j < m + 1; ++j) f[j] = min(f[j], f[j - c[i]] + 1); for (int j = m; j > 0; --j) if (f[j] < 0x3fffff00) {printf("%d", f[j]); break; } return 0;}