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 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 (≤, the total number of coins) and M (≤, 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​​ such that 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], ...} 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

题目分析:读题后觉得这是一道动态规划 自己写的动态规划只过了4个测试点 

查了别人的做法后 https://blog.csdn.net/qq_29762941/article/details/82903476
利用深度遍历优先也可以求解 而且正是因为需要最小的序列 利用dfs更容易求得
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Array[];
int N, M;
vector<int> res, v;
int flag = ;
void dfs(int i,int sum,int &flag)
{
if (sum > M || flag == )
return;
if (sum == M && flag == )
{
res = v;
flag = ;
return;
}
for (int index = i; index < N; index++) {
v.push_back(Array[index]);
dfs(index + , sum + Array[index], flag);
v.pop_back();
}
} int main()
{
cin >> N >> M;
int total=;
for (int i = ; i < N; i++)
{
cin >> Array[i];
total += Array[i];
}
if (total < M)
{
cout << "No Solution";
return ;
}
sort(Array, Array + N);
dfs(, , flag);
if (!flag)
cout << "No Solution";
else
{
int i = ;
for (; i < res.size() - ; i++)
cout << res[i] << " ";
cout << res[i];
}
}

动态规划的方法 看了柳神的博客

 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
#include<string.h>
using namespace std;
int Array[];
int N, M;
vector<int> res;
int dp[][];
bool choice[][];
bool compare(int a, int b) { return a > b; } void solve() {
sort(Array, Array + N, compare);
memset(dp, -, sizeof(dp));
memset(dp, false, sizeof(choice));
for(int i=;i<N;i++)
for (int j = ; j <= M; j++) {
if (j < Array[i])
dp[i + ][j] = dp[i][j];
else if (dp[i][j] > dp[i][j - Array[i]] + Array[i])
dp[i + ][j] = dp[i][j];
else
{
choice[i][j] = true;
dp[i + ][j] = dp[i][j - Array[i]] + Array[i];
}
}
if (dp[N][M] != M)
cout << "No Solution";
else {
int i = N, j = M;
while (i>=)
{
if (choice[i][j]) {
res.push_back(Array[i]);
j -= Array[i];
}
i--;
}
for (int i = ; i < res.size() - ; i++)
cout << res[i] << " ";
cout << res[res.size() - ];
}
} int main()
{
cin >> N >> M;
for (int i = ; i < N; i++)
cin >> Array[i];
solve();
return ;
}
 

最新文章

  1. 离线安装swashbuckle(webapi自动文档及测试工具)
  2. NOIP2012拓展欧几里得
  3. C++学习笔记33:泛型编程拓展2
  4. docker pull certification error
  5. audio和video元素
  6. popUpWindow 动画无法超出窗体的解决方案
  7. [转] Spring Boot and React hot loader
  8. 乱译文档--开始使用Musca
  9. PHP开发APP接口
  10. 以@Value方式注入 properties 配置文件
  11. android 缓存实现
  12. C 标准库基础 IO 操作总结
  13. Notepad++ 安装连接服务器的NppFTP插件
  14. Replace To Make Regular Bracket Sequence
  15. 正则表达式零宽断言详解(?=,?&lt;=,?!,?&lt;!)
  16. Android动态获取权限
  17. excel 单元格内容太多,替换有问题
  18. Swift学习笔记(十五)——程序猿浪漫之用Swift+Unicode说我爱你
  19. Spring+shiro配置JSP权限标签+角色标签+缓存
  20. slim(4621✨)

热门文章

  1. Linux命令之解压缩命令tar,zip,rar
  2. python学习的新篇章--面向对象
  3. 文本编辑器 - Sublime Text 3 换行无法自动缩进的解决方法
  4. VScode 格式化代码保存时使用ESlint修复代码
  5. 通过CGAL将一个多边形剖分成Delaunay三角网
  6. [日志分析]Graylog2采集mysql慢日志
  7. 分享几个 PHP 编码的最佳实践
  8. 【Weiss】【第03章】链表例程的一些修改
  9. VS2019 C++动态链接库的创建使用(3) - 如何导出类
  10. mvc+ef入门(三)