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. PHP攻击网站防御代码-以及攻击代码反译

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  2. Go语言:如何解决读取不到相对路径配置文件问题

    背景 项目交叉编译为可执行文件之后,在其他目录执行文件时提示找不到配置文件 2020/03/14 20:44:23 配置文件读取失败 open config.ini: no such file or ...

  3. Unsafe中CAS的实现

    前言 Unsafe 是位于 sun.misc 包下的一个类.Unsafe 提供的 API 大致可分为内存操作.CAS.Class 相关.对象操作.线程调度.系统信息获取.内存屏障.数组操作等几类.由于 ...

  4. Navicat15最新版本破解 亲测可用!!!

    1.下载Navicat Premium官网https://www.navicat.com.cn/下载最新版本下载安装 2.本人网盘链接:https://pan.baidu.com/s/1ncSaxId ...

  5. QT使用信号量QSemaphore处理大量数据

    实现如下:

  6. Linux软件安装之JDK的安装

    JDK的安装 1.1. 下载JDK,此处版本是1.8u131,实际操作以自己具体版本为准 先查看Linux系统是多少位(32位/64位):getconf LONG_BIT 然后去官网下载JDK [jd ...

  7. 第十一周Java实验作业

    实验十一   集合 实验时间 2018-11-8 1.实验目的与要求 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: Vector类类似长度可变的数组,其中只能存放对 ...

  8. 洛谷 P5596 【XR-4】题 题解

    原题链接 本题只要 推式子 就可以了. \[y^2-x^2=ax + b \] \[a x + x^2 = y^2 - b \] \[4 x^2 + 4 ax = 4 y^2 - 4b \] \[(2 ...

  9. JUnit 5基础指南

    A Guide to JUnit 5 准备 添加maven依赖: <dependency> <groupId>org.junit.jupiter</groupId> ...

  10. 【笔记3-31】Python语言基础-序列sequence

    序列sequence 可变序列 列表 list 字典 不可变序列 字符串 str 元祖 tuple 通过索引修改列表 del 删除元素 del my_list[2] 切片赋值只能是序列 .insert ...