Optimal Coin Change(完全背包计数)

简介: 题目描述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.

题目描述


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


题目大意:


给出一个钱的总数以及硬币的面额数,每种硬币的使用次数并没有限制,在保证能够用硬币得到总的金额的情况下,输出最小使用硬币的情况下每种硬币的使用个数,如果不能用所给出的硬币得到总的金额就输出 -1


对于前面判断能不能用所给面值的硬币得到总的金额用完全背包就很容易就可以解决,重点是如何进行路径的记录


int n, sum, a[maxn];
int ans[maxn], dp[maxn], cur[maxn];
int main()
{
    while (cin >> sum >> n) {
        memset(dp, 0x3f3f3f3f, sizeof(dp));
        memset(ans, 0, sizeof(ans));
        for (int i = 1; i <= n; i++) a[i] = read;
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = a[i]; j <= sum; j++) {
                if (dp[j - a[i]] < INF && dp[j - a[i]] < dp[j]) {
                    dp[j] = dp[j - a[i]] + 1;
                    cur[j] = j - a[i];///存下当前的
                }
            }
        }
        ///cout<<dp[sum]<<endl;
        if (dp[sum] == 0x3f3f3f3f) puts("-1");
        else {
            ll t = sum;///记录总数
            while (t) {
                ans[t - cur[t]] ++;///每一个的数量
                t = cur[t];
            }
            for (int i = 1; i <= n; i++) {
                printf("%lld", ans[a[i]]);
                if (i != n) printf(" ");
            }
            puts("");
        }
    }
    return 0;
}



目录
相关文章
|
9月前
|
算法
hdoj 3732 Ahui Writes Word (多重背包)
来看题目吧,可能有100000个单词,然后只有1000ms,但看包的大小,有10000,这样只能允许nlog(n)的算法,还有,每个单词的价值和花费都很小(不大于十),如果不考虑单词的不同,只考虑价值和花费只有最多100种东西,但如果把这些按多重背包的方法来计算依旧会超时,很容易想到和之前01背包的时间复杂度是一样的。
29 0
|
2月前
G - Prime Ring Problem(深搜)
G - Prime Ring Problem(深搜)
|
2月前
走楼梯2(Daimayuan Online Judge)
走楼梯2(Daimayuan Online Judge)
24 0
|
9月前
light oj 1231-1232 - 1233- Coin Change 背包
暂时到半懂不懂也没办法讲明白,就不误人子弟了,直接贴代码了。
23 0
|
11月前
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
31 0
LeetCode 322. Coin Change
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
65 0
LeetCode 322. Coin Change
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
|
人工智能 BI
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
107 0
HDOJ 1391 Number Steps(打表DP)
HDOJ 1391 Number Steps(打表DP)
108 0
HDOJ 1391 Number Steps(打表DP)
|
人工智能
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
86 0