PAT 1068 Find More Coins

ADS Project 5的问题是PAT 1068 Find More Coins

问题重现

Title: Find More Coins (30)

Description

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N ( N104 , the total number of coins) and M( M102 , the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification

For each test case, print in one line the face values V1,V2,...,Vk such that V1+V2+...+Vk=M . All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output “No Solution” instead.
Note: sequence A[1],A[2],... is said to be “smaller” than sequence B[1],B[2],... if there exists k1 such that A[i]=B[i] for all i<k , and A[k]<B[k] .

Samples

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution

解法

题目大意

给出N个硬币,每个硬币都有自己的价值,彼此可以相等也可以不相等。

问是否存在一个选择:选择其中若干个硬币使得其价值和恰好为M。

如果有回答这个选择序列;如果存在多解,回答字典序最小的那个。

如果没有这样的选择,回答“No Solution”。

化归

本题可以转化为一个背包问题,思考本题与一般的0-1背包的区别。

一般的0-1背包问题中,单件物品的价值 Vi 与重量 Wi 没有数量关系。

如果将本题中的硬币看成是物品,并假设它的价值与重量相等,背包容量是需要支付的钱。

那么问题的一部分就变为了“选择N个硬币,使得它们的价格不超过M”

iSelectionVi=MiSelectionViM

并且,可以按照0-1背包去求解:在重量(等于价值)不超过M的情况下,求最大能取得的价值。

状态转移方程:

f(i,j)={max{f(i1,j),f(i1,jV(i))+V(i)},0,i>0otherwise

在完成了时间复杂度为 O(MN) 的计算后,你获得了一个空间复杂度为 O(MN) 的解空间(通常存储在一个二维数组里)。

有的人会选择将空间优化为O(M),但这样会丢失路径信息,本题是需要求解路径的,这样做会得不偿失。
当然,有些题目还是压缩一下比较好,比如PTA 1045 Favorite Color Stripe

这个时候你需要判断最大取得的价值是否等于M:如果是,则至少有一解;否则无解。

如果题目只是问是否有解,那么解答到此结束,很可惜并不是,它还需要具体的解(路径),而且是字典序最小的路径。

解空间

讲个题外话,在动态规划这种利用解空间缓存答案的算法中。检查解空间的值 是调试动态规划算法的重要手段之一。

当然,算法的正确性主要还是依赖从数学上去证明。

方法当然是简单粗暴,对于一维、二维的解空间,直接打印出来
以二维解空间int dp[8][10] 为例。

for(int i = 0; i < 8; i++){
    for(int j = 0; j < 10; j++){
        printf("%d ", dp[i][j]);
    }
    printf("\n"); // end of line
}

它有可能会输出这样的玩意(本题样例1中的解空间)

0 0 0 0 0 0 0 0 0 9 
0 0 0 0 0 0 0 0 8 9 
0 0 0 0 0 0 0 7 8 9 
0 0 0 0 0 5 5 7 8 9 
0 0 0 0 4 5 5 7 8 9 
0 0 0 3 4 5 5 7 8 9 
0 0 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 

这样就可以直观地调试代码了,可以手动笔算一遍,如果不一样,看看从哪里开始有问题了。

二维以上的解空间就需要特殊的序列化(Serialization)手段了。

路径

本题求解路径是一个难点。首先,在一开始计算解空间的时候,就要将硬币按价格降序输入。

有的人会用一个另外的与解空间大小相当的空间来存储路径信息,其实根本就没有这个必要。因为路径的信息本来就存储在那个二维的解空间之中,它不曾离开。

路径回溯的算法如下:
有硬币的价格序列int V[N] 并已经降序,并假设问题有解。
对于二维解空间int dp[N][M + 1]来说,伪码如下

// dp[i][j] 表示 V[0 ... i) 可以达到的最大价值,不超过j
while M != 0
    find the maximum i in [0, N) that makes M == V[i] + dp[i - 1][M - V[i]]: 
    push V[i] into the solution array // V[i] is one of the expected serial
    // update N and M
    M = M - V[i]
    N = i

停机性

可以看到这个算法如果要结束,M必为0,根据有解的假设,若硬币序列V[i]选择正确,则停机性成立。

选择正确性

选择一个最大的 i,由于 V 是降序的,因此满足条件的 V[i] 是最小的,满足题设要求的字典升序。

回顾计算解空间的算法:

dp[i][j]=max{dp[i1][j],dp[i1][jV[i]]+V[i]}

反解得: 当 dp[i][j] == dp[i - 1][j - V[i]] + V[i] 时说明 V[i] 是所求的硬币之一。

将 j 代入为 M:

dp[i][M]=V[i]+dp[i1][MV[i]]

又因为有解的条件 dp[i][M]=M ,得

M=V[i]+dp[i1][MV[i]]

即可得到上述伪码中的式子,可知 V[i] 即为所求硬币。

递归求解

更新 N, M 的值本质上是递归求解。递归的边界条件是 M == 0 (价值耗尽)
每当找到一个硬币的值V[i]时,要抛弃所有比V[i]小的硬币(他们并不满足选择正确性的公式,不可能是答案序列的成员),并且将M更新为扣除V[i]剩余的价值,然后V[i]自身也要被丢弃。

M = M - V[i] // 取剩余价值
N = i // 抛弃V[i ... N)

为了更好说明它的递归性质,有递归的伪码如下:

solve(N, M)
    if M == 0 exit
    find the maximum i in [0, N) that makes M == V[i] + dp[i - 1][M - V[i]]: 
    push V[i] into the solution array // V[i] is one of the expected serial
    call solve(i, M - V[i])

讲解样例1

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5
  1. 降序排序硬币价格

    9 8 7 5 4 3 2 1

  2. 计算解空间 dp[8][10] 如下

    0 0 0 0 0 0 0 0 0 9 
    0 0 0 0 0 0 0 0 8 9 
    0 0 0 0 0 0 0 7 8 9 
    0 0 0 0 0 5 5 7 8 9 
    0 0 0 0 4 5 5 7 8 9 
    0 0 0 3 4 5 5 7 8 9 
    0 0 2 3 4 5 6 7 8 9 
    0 1 2 3 4 5 6 7 8 9     
  3. 回溯求路径

    // solve(8, 9)
    N = 8, M = 9;
    i = N - 1; // i == 7
    dp[7 - 1][9 - V[7]] + V[7] == 9; // 8 + 1 == 9 成立,输出V[7] = 1
    M = M - V[7]; // M == 8
    N = i; // N == 7
    // solve(7, 8)
    N = 7, M = 8;
    i = N - 1; // i == 6, V[6] == 2 
    dp[6 - 1][8 - V[6]] + V[6] == 8; // 5 + 2 == 8 不成立
    i--; // i == 5, V[5] == 3
    dp[5 - 1][8 - V[5]] + V[5] == 8; // 5 + 3 == 8 成立,输出V[5] = 3
    M = M - V[5]; // M == 5
    N = i; // N == 5
    // solve(5, 5)
    N = 5, M = 5;
    i = N - 1; // i == 4, V[4] == 4
    dp[4 - 1][5 - V[4]] + V[4] == 5; // 0 + 4 == 5 不成立
    i--; // i == 3, V[3] == 5
    dp[3 - 1][5 - V[3]] + V[3] == 5; // 0 + 5 == 5 成立,输出V[3] = 5
    M = M - V[3]; // M == 0
    N = i; // N == 3
    exit; // 结束 输出 1 3 5

    图解:

    0 0 0 0 0 0 0 0 0 9 
    0 0 0 0 0 0 0 0 8 9 
    0 0 0 0 0 0 0 7 8 9 
    0 0 0 0 0 5 5 7 8 9 
    0 0 0 0 4 5 5 7 8 9 
    0 0 0 3 4 5 5 7 8 9 
    0 0 2 3 4 5 6 7 (8) 9 
    0 1 2 3 4 5 6 7 8 [9] // 8 + 1 == 9 
    0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 8 
    0 0 0 0 0 0 0 7 8 
    0 0 0 0 0 5 5 7 8  
    0 0 0 0 4 (5) 5 7 8  
    0 0 0 3 4 5 5 7 8  // 5 + 3 == 8
    0 0 2 3 4 5 6 7 [8] 
    0 0 0 0 0 0  
    0 0 0 0 0 0  
    (0) 0 0 0 0 0  
    0 0 0 0 0 5   // 0 + 5 == 5
    0 0 0 0 4 [5]

通过代码

PAT 1068

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值