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 ;
}
 

1068 Find More Coins (30分)(dp)的更多相关文章

  1. PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

    1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some ...

  2. 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

    题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...

  3. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

  4. 1068 Find More Coins (30)(30 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  5. PAT (Advanced Level) 1068. Find More Coins (30)

    01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...

  6. 1068. Find More Coins (30)

    题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

  7. L3-020 至多删三个字符 (30 分)(DP)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 学习地址: 2018CCCC-L3 ...

  8. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  9. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

随机推荐

  1. 解决 微信包含emoji表情的昵称,直接用sql语句可以写入而在yii2框架写却写不成功的 问题

    背景: 首先是emoji表情写入不成功,在网上查了许多资料,包括配置mysql,重启mysql等等,这样之后发现还是写入失败. 将sql语句复制出来,直接粘贴到mysql客户端执行,发现没问题.而通过 ...

  2. MySQL数据备份及还原(一)

    关于删库跑路的事故现在已经屡见不鲜了,数据备份的必要性是企业数据管理极其重要的一项工作.关于数据备份.恢复也有很多场景及方法,本系列也会将主要的几种工具通过案例进行演示. 本系列将从逻辑备份及恢复开始 ...

  3. Java-字符输入输出(新手)

    参考手册: BufferedReader BufferedWriter: 关键字: close() 关闭流,先刷新.    newLine() 写一行行分隔符.    write() 写一个字符    ...

  4. 手把手教你用GoEasy实现Websocket IM聊天

    经常有朋友问起GoEasy如何实现IM,今天就手把手的带大家从头到尾用GoEasy实现一个完整IM聊天,全套代码已经放在了github. 今日的前端技术发展可谓百花争鸣,为了确保本文能帮助到使用任何技 ...

  5. Spring MVC系列-(3) Bean的装配

    3. 高级装配Bean 3.1 Bean的作用域 默认情况下,Spring中的bean都是以单例的形式存在的,无论注入多少次,每次注入的都是同一个实例. 考虑到某些bean可能是可变的,Spring定 ...

  6. canvas绘制流星雨特效

    源码: <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta n ...

  7. jq 导航跟随 模拟京东手机端

    想做一个导航跟随,但是代码都要下载,自己简单些了一个,css都放html里面了,所以也不用下载直接新建html,然后粘贴,点开就是导航跟随效果 效果如图 <!DOCTYPE html> & ...

  8. Prism 源码解读2-View的加载和控制

    介绍 上一篇介绍了Region,这一篇跟Region息息相关,讲一下Region中View的加载方式及控制. 4.ViewDiscovery 在创建好Region后需要将View添加到Region中. ...

  9. LeetCode45——从搜索算法推导到贪心

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode系列的第25篇文章,今天我们一起来看的是LeetCode的第45题,Jump Game II. 有同学后台留言问我说, ...

  10. 蓝桥杯——一步之遥,扩展gcd的应用

    1. 一步之遥 [问题描述]从昏迷中醒来,小明发现自己被关在X星球的废矿车里.矿车停在平直的废弃的轨道上.他的面前是两个按钮,分别写着“F”和“B”. 小明突然记起来,这两个按钮可以控制矿车在轨道上前 ...