PAT 1068. Find More Coins (30)(背包问题,动态规划)

官网

1068. Find More Coins (30)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 (<=104, the total number of coins) and M(<=102, 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 k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].

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

题目大意

  • 1.问你从给出的一组数列中能不能找出几个数的和等于m,如果有,输出数值最小的那一种。
  • 2.都说知道背包问题这道题就很简单了,然而像我这种,之前都没接触这些算法,花了很长的时间才看懂这些问题。简谈动态规划

解题思路(参考自闲云阁

  • 1.已经是第二次写这道题了,希望没接触过动态规划的人也能看懂这道题,做会这道题。
  • 2.先用这道题的数据列一个表格,其中这道题是要凑出9,给出的数列是5 9 8 7 2 3 4 1,从大到小排列后是9 8 7 5 4 3 2 1。
Tables123456789
9000000009
98000000089
987000000789
9875000055789
98754000455789
987543003455789
9875432023456789
98754321123456789
  • 3.这个表格代表什么意思呢,例如第一行为9,纵坐标为1时,她的值为零,代表假如这个数列只有9一个数,能取到不大于纵坐标1的数列和最多就是0。例如到纵坐标为9了,第一行能取到的数最大就是9了,以此类推,可以填完整个表格。
  • 4.如何填这个表格,例如第二行比第一行多了一个8,那么在i = 2,j = 8时,代表从98俩个数中,能取到不大于8的数字最大能取到8了,这很明显,但怎样用算法描述出来呢?并且能够有效快捷方便的去计算这个表呢?
  • 5.F[i][j]用来表示这个表,表示从排好序的序列a[i]的前i个数字中,能取到不超过j的最大的值,那么我们有F[i][j] = max{F[i-1][j-a[i]]+[],F[i-1][j]},即要求F[i][j],假如我们要这个元素,那么我们最大能从第i-1个元素中取j-a[i],在加上a[i],即代表要这个元素了,加入不要这个元素,那么F[i][j] 就与F[i-1][j]相同了,总之,我们取他们的最大值即为所求根。
  • 6.用这种方式填完这个表格接下来就好办了,假如F[n][m] == m,就代表有解,并且我们在求F[i][j]的过程中,记录取到最大值需不需要第i个元素,然后回溯输出即可。再说明一点,从大到校排序是为了能够输出最小的数值。具体看代码。

AC代码

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
int n,m;
int Find_n_m[10002][102];
int needI_n_m[10002][102];
int a[10002];
vector<int> keep;
int main(int argc, char *argv[])
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    //从大到小排列
    sort(a+1,a+n+1,greater<int>());
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            //取要它和不要它俩种结果的最大值,如果要它并把它记录下来
            //另外加入 加了它比前任大了,但是超过n了,也不要的,所以还要判断它a[i]是否大于j
            if (Find_n_m[i-1][j - a[i]] + a[i] < Find_n_m[i - 1][j] || a[i] > j) {
                Find_n_m[i][j] = Find_n_m[i - 1][j];
            }else {
                Find_n_m[i][j] = Find_n_m[i-1][j - a[i]] + a[i];
                //取到最大值需要它,把它记录下来
                needI_n_m[i][j] = 1;
            }
        }
    }
    if (Find_n_m[n][m] == m) {
        //用记录下来的值回溯输出
        //如果不要第n个数,就--
        while (m != 0) {
            while (needI_n_m[n][m] == 0) {
                n--;
            }
            keep.push_back(a[n]);
            m-=a[n];
            n--;
        }
        for (int i = 0; i < keep.size(); ++i) {
            if (i == 0) {
                cout << keep[i];
            }else {
                cout << " " << keep[i];
            }
        }
        cout << endl;
    }else {
        cout << "No Solution" << endl;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值