1068 Find More Coins
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 V1≤V2≤⋯≤Vk such that V1+V2+⋯+Vk=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的更多相关文章
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- 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 ...
- 1068. Find More Coins (30)
题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- PAT 甲级 1068 Find More Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...
- 1068 Find More Coins (30)(30 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 1068 Find More Coins (30分)(dp)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...
随机推荐
- docker封装nuxt项目使用jenkins发布
一.概述 vue项目可以打一个dist静态资源包,直接使用Nginx发布即可. 但是nuxt项目无法像vue那样,可以打一个dist静态资源包. 需要安装Node.js,并使用npm install ...
- vivo 官网资源包适配多场景的应用
本文介绍了资源包的概念及使用场景,同时对资源包的几种使用方案进行对比.通过本文,大家可以快速掌握资源包的使用方法,解决单一配置满足多场景.多样式的问题. 一.业务背景 随着官网项目的业务深入发展,单纯 ...
- 如何读写拥有命名空间xmlns 属性的Xml文件(C#实现)
我们在进行C#项目Xml读写开发时经常遇到一些读写问题,今天我要介绍的是遇到多个命名空间xmlns属性时如何读写此类文件. 比如下面这个Xml文件: <?xml version="1. ...
- LightOJ-1074(SPFA判负圈+Bellman-Ford算法)
Extended Traffic LightOJ-1074 这题因为涉及到减法和三次方,所以可能会出现负圈. 这里使用的算法叫做SPFA算法,这个可以用来判负圈和求解最短路.Bellman-Ford算 ...
- JavaWeb随笔整理
JavaWeb随笔整理 为方便阅读,故整理了相关学习笔记 前端相关 HTML CSS JavaScript BootStrap 数据库相关 MySQL基础 MySQL表的约束和数据库设计 MySQL多 ...
- Flutter Web 支持现已进入稳定版
作者 / Mariam Hasnany, Product Manager, Flutter 我们对 Flutter 的愿景是成为一个可移植的 UI 框架,在全平台上构建精美的应用体验.做为 Flutt ...
- struts2.0中ognl栈的解析
ongl详解: ValueStack是Struts2的一个接口,字面意义为值栈,OgnlValueStack是 ValueStack的实现类,客 户端发起一个请求,struts2架构会创建一个acti ...
- 2019 GDUT Rating Contest I : Problem A. The Bucket List
题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...
- 跨端开发技术 | 拼团商城项目同时开发app和小程序的要点
此项目为拼团商城类型,主要功能包括商品分类.商品详情.商品搜索.拼团.订单管理等. 项目源码在 https://github.com/apicloudcom/group-ec 仓库的 widget 目 ...
- Git修改用户名、邮箱和密码
$ git config --global --replace-all user.name "要修改的用户名" $ git config --global --replace-al ...