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

题意:

  在给出的n个硬币中,选出几个硬币,使选出的这些硬币之和为m。如果存在多个解,则输出序列较小的那个。

思路:

  这是一道01背包的问题,根据背包的容量来判断当前的硬币是选还是不选,如果选取的话就要更新背包的容量状态,另外用select[i][j]来表示当背包体积为j的时候,物品i是否在背包中。用来最后判断背包中的物品。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int n, m;
7 cin >> n >> m;
8 vector<int> coins(n+1, 0);
9 vector<int> dp(m+5, 0);
10 bool choice[10005][105] = {false};
11 for (int i = 1; i <= n; ++i) cin >> coins[i];
12 sort(coins.begin()+1, coins.end(), greater<int>());
13 for (int i = 1; i <= n; ++i) {
14 for (int j = m; j >= coins[i]; --j) {
15 if (dp[j] <= dp[j - coins[i]] + coins[i]) {
16 dp[j] = dp[j - coins[i]] + coins[i];
17 choice[i][j] = true;
18 }
19 }
20 }
21 if (dp[m] != m)
22 cout << "No Solution";
23 else {
24 vector<int> ans;
25 int volume = m, index = n;
26 while (volume > 0) {
27 if (choice[index][volume]) {
28 ans.push_back(coins[index]);
29 volume -= coins[index];
30 }
31 --index;
32 }
33 for (int i = 0; i < ans.size(); ++i) {
34 if (i == 0)
35 cout << ans[i];
36 else
37 cout << " " << ans[i];
38 }
39 }
40 cout << endl;
41 return 0;
42 }

1068 Find More Coins的更多相关文章

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

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

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

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

  3. 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 ...

  4. 1068. Find More Coins (30)

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

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

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

  6. PAT 甲级 1068 Find More Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...

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

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

  8. 1068 Find More Coins (30分)(dp)

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

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

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

随机推荐

  1. MySQL:事务机制

    为什么需要事务处理? 在执行SQL语句的时候,某些业务要求,一系列操作必须全部执行,而不能仅执行一部分. MySQL5.0后引入了事务机制,MySQL支持几种基本的数据库引擎,并非所有引擎都支持事务处 ...

  2. 常用linux命令,开发必备-速收藏

    在前面我们介绍了通过VirtualBox安装Linux的方法,参考: 一网打尽,一文讲通虚拟机VirtualBox及Linux使用 本文我们将介绍在使用linux的过程中常用的一些Linux命令,掌握 ...

  3. 2020年HTML5考试模拟题整理(一)

    1.哪个元素被称为媒体元素的子元素? 答案:<track>. <track> 标签为媒体元素(比如 <audio> and <video>)规定外部文本 ...

  4. Java 查找算法

    1 查找算法介绍 在 java 中,我们常用的查找有四种: 1) 顺序(线性)查找 2) 二分查找/折半查找 3) 插值查找 4) 斐波那契查找   2 线性查找算法 有一个数列: {1,8, 10, ...

  5. add_header被覆盖 -配置错误

    Nginx的配置文件分为Server.Location.If等一些配置块,并且存在包含关系,和编程语言比较类似.如果在外层配置的一些选项,是可以被继承到内层的. 但这里的继承也有一些特性,比如add_ ...

  6. 七种方案!探讨Redis分布式锁的正确使用姿势

    前言 日常开发中,秒杀下单.抢红包等等业务场景,都需要用到分布式锁.而Redis非常适合作为分布式锁使用.本文将分七个方案展开,跟大家探讨Redis分布式锁的正确使用方式.如果有不正确的地方,欢迎大家 ...

  7. Cloudam云端,探索高性能计算在药物研究领域的解决方案

    近日,Cloudam云端与国内某知名药企与合作,通过接入Cloudam云端自主研发的云E云超算服务,计算效率提高的数百倍.这也是云算力在生命科学领域的又一次成功应用.Cloudam云端云E云超算服务是 ...

  8. 【java框架】MyBatis-Plus(1)--MyBatis-Plus快速上手开发及核心功能体验

    1.MyBatis-Plus入门开发及配置 1.1.MyBatis-Plus简介 MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变, ...

  9. 关于github的使用学习心得

    先写先介绍一下如何用github上创建一个项目吧. 用户登录后的界面如上所示.右下角是我们已经建好的库.点击其中任何一个就可以查看相应的库了.如果要新建一个项目的话,就点击Start a projec ...

  10. 从设计模式角度看OkHttp源码

    前言 说到源码,很多朋友都觉得复杂,难理解. 但是,如果是一个结构清晰且完全解耦的优质源码库呢? OkHttp就是这样一个存在,对于这个原生网络框架,想必大家也看过很多很多相关的源码解析了. 它的源码 ...