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 ...
随机推荐
- Go的map
目录 map 一.map的创建 1.map的类型 2.定义并初始化 二.给map添加元素 三.获取map的元素 四.删除map的元素 五.获取map的长度 六.map的类型 七.map的相等性 八.循 ...
- 2021-2-28:调用 System.gc() 后究竟发生了什么?
首先,根据 DisableExplicitGC 这个 JVM 启动参数的状态,确定是否会 GC,如果需要 GC,不同 GC 会有不同的处理. 1. G1 GC 的处理 如果是 System.gc() ...
- 后端程序员之路 28、一个轻量级HTTP Server的实现
提到http server,一般用到的都是Apache和nginx这样的成熟软件,但是,有的情况下,我们也许也会用一些非常轻量级的http server.http server的c++轻量级实现里,M ...
- CentOS rpm常用功能记录
CentOS7主要有rpm和yum这两种包软件的管理.两者有功能上的区别,其中主要区别是:yum使用简单但需要联网,yum会去网上包源去获取所需要的软件包.而rpm的需要做的事情就更细一些,比如我们需 ...
- 2020年12月-第02阶段-前端基础-CSS基础选择器
CSS选择器(重点) 理解 能说出选择器的作用 id选择器和类选择器的区别 1. CSS选择器作用(重点) 如上图所以,要把里面的小黄人分为2组,最快的方法怎办? 很多, 比如 一只眼睛的一组,剩下的 ...
- 如何报告FreeBSD 的bug?
https://bugs.freebsd.org/bugzilla/ 注册个账号即可,请使用英语,把程序在不同程序上的运行结果列出来即可- 注意标记架构,如果有log还请一并附上,英语差可以 ...
- 提升Idea启动速度与Tomcat日志乱码问题
提升Idea启动速度与Tomcat日志乱码问题 前言 由于重装了一次Idea,所以有些设置时间就忘了,在此做个记录,以便以后忘记后可以来翻阅 Idea启动速度 一.将Idea所在的 安装文件夹 在wi ...
- Beego框架学习---layout的使用
Beego框架学习---layout的使用 在管理系统中,管理菜单的界面是固定的,会变化的只是中间的部分.我就在想,是不是跟angular的"组件模块的router-outlet一样&quo ...
- java中常见的内存泄露的例子
JAVA 中的内存泄露 Java中的内存泄露,广义并通俗的说,就是:不再会被使用的对象的内存不能被回收,就是内存泄露. Java中的内存泄露与C++中的表现有所不同. 在C++ ...
- 使用Java+NetBeans设计web服务和页面,用Tomcat部署网页
一 安装NetBeans(自动安装jdk) 二 创建服务器 三 发布服务 一 安装NetBeans(自动安装jdk) 进入oracle的下载界面: http://www.oracle.com/tech ...