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. RabbitMQ基础教程

    目录 RabbitMQ相关概念介绍 生产者和消费者 队列 交换器.路由键.绑定 交换器类型 RabbitMQ运转流程 AMQP协议介绍 AMQP生产者流转过程 AMQP消费者流转过程 安装Rabbit ...

  2. Ch1-What is DAX?

    What is DAX? 数据分析表达式 (DAX) 是在 Analysis Services.Power BI 以及 Excel 中的 Power Pivot 使用的公式表达式语言.在第一版Powe ...

  3. MySQL:逻辑库与表管理

    逻辑库管理 语句 说明 CREATE DATABASE 逻辑库名; 创建逻辑库 SHOW DATABASES; 显示所有逻辑库 DROP DATABASE 逻辑库名; 删除逻辑库 USE 逻辑库名; ...

  4. AOP +FreeSql 跨方法异步事务

    AOP +FreeSql 跨方法异步事务 Autofac.Extensions.DependencyInjection Autofac.Extras.DynamicProxy Castle.Core. ...

  5. CentOS Install NMP

    目录 Installation steps of the Nginx install run 默认安装路径 指定安装目录 Installation steps of the MySQL 下载源码包 解 ...

  6. C++指针的算术运算 、关系运算

    下面随笔是关于指针的算术运算 .关系运算. 指针类型的算术运算 指针与整数的加减运算 指针++,--运算 指针类型的算术运算 指针p加上或减去n 其意义是指针当前指向位置的前方或后方第n个数据的起始位 ...

  7. C++类的友元机制说明

    下面给出C++类的友元机制说明(对类private.protected成员访问),需要注意的是,友元机制尽量不用或者少用,虽然它会提供某种程度的效率,但会带来数据安全性的问题. 类的友元 友元是C++ ...

  8. CCF(元素选择器:50分):字符串+模拟

    元素选择器 201809-3 这里我只考虑了没有后代选择器的情况 #include<iostream> #include<cstdio> #include<cstring ...

  9. 翻译:《实用的Python编程》03_05_Main_module

    目录 | 上一节 (3.4 模块) | 下一节 (3.6 设计讨论) 3.5 主模块 本节介绍主程序(主模块)的概念 主函数 在许多编程语言中,存在一个主函数或者主方法的概念. // c / c++ ...

  10. Java I/O流 02

    IO流·字节流 IO流概述及其分类 * A:概念 * IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流操作的 * Java用于操作流的类都在IO包中 * 流按流向分为两种输入流.输出 ...