UPC-5561 Optimal Coin Change(DP)

题目描述
In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins.
In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.
The input includes a value v, a size of the coinage set n, and a face value of each coin, f1, f2, …, fn. The output is a list of numbers, namely, c1, …, cn, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value v is an integer satisfying 0 < v ≤ 2000, representing the change required
in cents. The face value of a coin is less than or equal to 10000. The output of your program should take the combination with the least number of coins needed.
For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 10 cents, 20 cents, 50 cents, 1 dollar, 2 dollars, 5 dollars and 10 dollars would be represented in the input by n = 7, f1 = 10, f2 = 20, f3 = 50, f4 = 100, f5 = 200, f6 = 500, f7 = 1000.
输入
The test data may contain many test cases, please process it to the end of the file.
Each test case contains integers v, n, f1, …, fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < …< fn.
输出
The output be n numbers in a line, separated by space. If there is no possible change, your output should be a single −1. If there are more than one possible solutions, your program should output the one that uses more coins of a lower face value.
样例输入
2000 7 10 20 50 100 200 500 1000
250 4 10 20 125 150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80
样例输出
0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2
3 0 0 1
1 1 0 1 0

题意:给出总价值,以及面值的种类数量,以及每个面值。现在要用尽量少的钱,用较大的面值来凑够总价值的钱。
动态规划记录从某个面值开始到达总价值,每个钱数所用到的面值,也就是说从价值为0开始,我第一张选的是某个面值,然后遍历过程中,看哪个钱数的时候能和上次用到某个面值的钱的记录凑上,直到凑到总价值的钱数,这个过程中记录凑钱的路径,注意遍历的过程是从较小面值开始的,也就是说是先尝试用较小面值凑总价值,然后用较大面值补上一些多余的小面值钱数,在这段过程中,有的小面值钱数可能会被大面值优化,因此在比较时用的是>=来判断是否更新。
然后根据记录的路径,回溯一遍,每次用到了哪个面值的钱,计数就++,最后得到的即总面值的钱是如何凑出来的。

#include<bits/stdc++.h>///DP
using namespace std;
const int maxn=10004;
int cnt[maxn],a[maxn],val[maxn],fa[maxn],ans[maxn];
int main()
{
    int sum,n;
    while(scanf("%d%d",&sum,&n)!=EOF)
    {
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        memset(cnt,0x3f,sizeof(cnt));
        memset(ans,0,sizeof(ans));
        cnt[0]=0;
        for(int i=n-1;i>=0;i--)
        {
            for(int j=a[i];j<=sum;j++)///遍历每个面值能的价格
            {
                if(cnt[j]>=cnt[j-a[i]]+1)///取等于则按较少钱币分配,否则按较多钱币分配
                {///若能延续用这么多钱
                    cnt[j]=cnt[j-a[i]]+1;///在此标记记录
                    fa[j]=j-a[i];///记录到达sum的路径
                    val[j]=i;///对于每个位置用了哪个面值的记录
                }
            }
        }
        if(cnt[sum]==cnt[sum+3])///如果未曾凑到sum说明怎么都凑不够
        {
            printf("-1
");
            continue;
        }
        while(sum)///否则按照路径记录对用到的每个面值计数
        {
            ans[val[sum]]++;
            sum=fa[sum];
        }
        for(int i=0;i<n;i++)printf("%d%c",ans[i],i==n-1?'
':' ');
    }
}
原文地址:https://www.cnblogs.com/kuronekonano/p/11135798.html