1068 Find More Coins (30 分)背包问题

版权声明:假装这里有个版权声明…… https://blog.csdn.net/CV_Jason/article/details/86033808

题目

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 1 0 4 10^4 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 (≤ 1 0 4 10^4 ,the total number of coins) and M (≤ 1 0 2 10^2 , 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 V 1 + V 2 + . . . + V k = M V_1+V_2+...+V_k = 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 ] , . . . {A[1], A[2], ...} is said to be “smaller” than sequence B [ 1 ] , B [ 2 ] , . . . {B[1], B[2], ...} if there exists k 1 k≥1 such that A [ i ] = B [ i ] A[i]=B[i] for all i < k i<k , and A [ k ] < B [ k ] 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

解题思路

  题目大意: 给定N个硬币,以及对应的面值,以及要付的款M,找出能够满足付款M面值的最小排序硬币组合 。
  解题思路: 这道题可以看做是一道动态规划的背包问题,先找出所有满足条件的硬币组合,然后再找出对应的最小排序的硬币组合即可。
  第一步,动态规划找到所有的符合条件的硬币组合,可以把硬币作为背包问题中的物件,数组coin是硬币的面值,相当于背包问题中,每个物件中的重量,那么要付的款项M就是背包问题中背包的总容量。那么现在问题就转化为,M容量的背包,放 c o i n [ i ] coin[i] 硬币的面值,能放多少个硬币。状态方程可以描述为;
   (1) F ( N , M ) = m a x { F ( N 1 , M ) , F ( N 1 , M c o i n [ i ] ) + c o i n [ i ] } F(N,M)=max\{F(N-1,M),F(N-1,M-coin[i])+coin[i]\} \tag{1}
  其中, F ( N , M ) F(N,M) 表示总面值不超过M,挑选到第N个硬币的时候,所能得到的最大面值总和。 c o i n [ i ] coin[i] 表示第i个硬币的面值。注意约束条件,不超过M,还有状态条件挑选到第N个硬币
  在这道题中,并没有限制挑选硬币的个数,因此可以把问题简化为——
   (2) F ( M ) = m a x { F ( M ) , F ( M c o i n [ i ] ) + c o i n [ i ] } F(M) = max\{F(M),F(M-coin[i])+coin[i]\} \tag{2}
  我们先对硬币面值从大到小进行排序,这样能够保证提要所要求的smallest sequence方便第二部的筛选。排序之后,我们从大到小进行遍历,考察每个硬币,放还是不放进去,这样 F ( s ) F(s) 就变成了约束面值为 s s 的时候,所能达到的最大值面值。同时,用一个数组 c h o i c e R e c o r d [ i ] [ j ] choiceRecord[i][j] 记录选择路径,在进行动态规划判断第i个硬币是否符合要求的时候,同时记录选择过程,数组 c h o i c e R e c o r d [ i ] [ j ] choiceRecord[i][j] 表示约束面值为 j j 的时候,是否选择第 i i 个硬币。
  第二步,筛选出最小序列,先判断是否有满足条件的序列,如果 F ( M ) ! = M F(M)!=M ,即约束条件为M的情况下,所能达到的最大面值不为 M M ,显然没有合适的硬币组合,输出No Solution .否则,按照硬币面值从小到大开始筛选.

/*
** @Brief:No.1068 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2019-01-07
** @Solution: Accepted!
*/
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int choiceRecord[100010][110];
int coin[100010];
int dp[110];
int main(){
	int N,M;
	scanf("%d%d",&N,&M);
	for(int i=1;i<=N;i++){
		scanf("%d",&coin[i]);
	}
	sort(coin+1,coin+N+1,[](int a,int b){return a>b;});
	for(int i=1;i<=N;i++){
		for(int j = M;j>=coin[i];j--){
			if(dp[j]<=dp[j-coin[i]]+coin[i]){
				choiceRecord[i][j] = true;
				dp[j] = dp[j-coin[i]]+coin[i];
			}
		}
	} 
	if(dp[M]!=M) printf("No Solution\n");
	else{
		vector<int> res;
		int request = M,index = N;
		while(request>0){
			if(choiceRecord[index][request]==true){
				request-=coin[index];
				res.push_back(coin[index]);
			}
			index--;
		}
		for(int i=0;i<res.size()-1;i++){
			printf("%d ",res[i]); 
		}
		printf("%d\n",res[res.size()-1]);
	} 
	return 0;
}

在这里插入图片描述

总结

  对动态规划不是很熟悉,这道题看了别人的代码,看了半天才看懂,果然还是自己太菜了,又强行复习了一波01背包问题,原来还可以这么用,一脸懵逼……

参考链接

[1]. https://blog.csdn.net/tiantangrenjian/article/details/17334201
[2]. https://blog.csdn.net/gzxcyy/article/details/14025949
[3]. https://blog.csdn.net/xyt8023y/article/details/47255241
[4]. https://baike.baidu.com/item/背包问题/2416931?fr=aladdin

猜你喜欢

转载自blog.csdn.net/CV_Jason/article/details/86033808