codevs 3961 coin change [complete backpack DP/memorized search]

Topic  Description

    In real life, we often encounter the problem of coin change. For example, when paying wages, financial personnel need to calculate the minimum number of coins for change, so that they can get the minimum number of coins from the bank and ensure that they can be used. These coins pay wages. We should note that the coin system of RMB is 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01 yuan, using these coins we can use the greedy algorithm for any salary number Find its minimum number of coins. But unfortunately: we probably don't have such a good coin system, so the greedy algorithm can't find the minimum number of coins, and even some money totals can't be changed with these coins. For example, if the coin system is $40, $30, $25, then $37 cannot be used for change; the minimum number of coins to change for $95 is 3. For another example, if the coin system is 10, 7, 5, and 1 yuan, then the number of coins obtained by greedy method for 12 yuan is 3, and the minimum number of coins is 2. 

    Your task is: for an arbitrary coin system and an amount of money, please program to find the minimum number of coins for change; if you can't make change with these coins, please give a method of change that minimizes the amount of money left . 

 

Input Description  Input Description

    The first row is N and T, where 1≤N≤50 is the number of different coins in the coin system; 1≤T≤100000 is the total number of coins that need to be changed. 

    Line 2 contains N positive integers not greater than 65535, which are the denominations of coins in the coin system. 

 

Output  Description

    If T can be changed by coins in the coin system, please output the minimum number of coins for change. 

    If T cannot be changed by coins in the coin system, please output the minimum number of coins in the change scheme with the least amount of money left. 

 

Sample  Input

    4 12 

    10 7 5 1

 

Sample  Output

    2

Data  Size & Hint

Bronze water problem

【Question meaning】:Question meaning: Enter n and m first, then enter n kinds of coins in the next line, each of which has a face value. Ask the minimum number of coins needed to form m. Complete knapsack problem. Although the question involves whether it can be composed, when it is possible, it is like the first set of data, the minimum number of coins is 2,  and when it cannot be composed, it is like the second set of data, supplement the number of existing coins in denomination, and see that the minimum number when it reaches m quantity 

【Code】:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<stack>
#define maxn 100005
#define maxm 505
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;


int a[55];
int dp[maxn];

intmain ()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=0;i<=m;i++)
            dp[i]=INF;
        dp[0]=0;
        for(int i=1;i<=n;i++){
            for(int j=a[i];j<=m;j++){
                dp[j]=min(dp[j],dp[j-a[i]]+1);
            }
        }
        printf("%d\n",dp[m]);
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324979616&siteId=291194637