PAT1048. Find Coins(01背包问题动态规划解法)
问题描述:
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 could only use exactly two coins to pay the exact amount. Since she has as many as 105 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 two 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 (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output "No Solution" instead.
Sample Input 1:
- 8 15
- 1 2 8 7 2 4 11 15
Sample Output 1:
- 4 11
Sample Input 2:
- 7 14
- 1 8 7 2 4 11 15
Sample Output 2:
- No Solution
解法描述:
本题可看把需要支付的总额看做背包,每个钱币当做需要放进背包中的物品,钱币的重量和价值均为钱币面额,所以这是一个典型的01背包问题,用动态规划方法依次把各个钱币加入解法中,计算每种总额能拼凑出的最大价值。
代码
- #include<algorithm>
- #include<iostream>
- using namespace std;
- int coins[] = { };
- int n, m;
- int dp[] = { };
- int ch[][] = { }; //ch[n][m]记录每次更新最大价值时,是否选择新加入的钱币
- bool cmp(int a, int b) {
- return a > b;
- }
- int main() {
- cin >> n >> m;
- for (int i = ; i < n; i++)
- {
- cin >> coins[i];
- }
- sort(coins, coins + n, cmp); //对钱币从大到小排序,因为要输出最小钱币的方案
- for (int i = ; i < n; i++)
- {
- for (int j = m; j >= ; j--)
- {
- if (j >= coins[i]) {
- if (dp[j] <= dp[j - coins[i]] + coins[i]) {
- ch[i][j] = ; //若选择了新加入的钱币,则ch[n][m]置为1
- dp[j] = dp[j - coins[i]] + coins[i];
- }
- }
- }
- }
- if (dp[m] != m) { //m总额下的最大价值不是m,则无解决方案
- cout << "No Solution" << endl;
- }
- else {
- for (int i = n - ; i >= ; i--) //从最小的钱币开始倒序遍历
- {
- if (ch[i][m]) { //若该钱币在拼凑方案中,则总额变为m-coins[i]
- m -= coins[i];
- cout << coins[i];
- if (m == ) {
- cout << endl;
- }
- else {
- cout << " ";
- }
- }
- }
- }
- return ;
- }
PAT1048. Find Coins(01背包问题动态规划解法)的更多相关文章
- c语言数据结构:01背包问题-------动态规划
两天的时间都在学习动态规划:小作业(01背包问题:) 数据结构老师布置的这个小作业还真是让人伤头脑,自己实在想不出来了便去网上寻找讲解,看到一篇不错的文章: http://www.cnblogs.co ...
- 0-1背包问题——动态规划求解【Python】
动态规划求解0-1背包问题: 问题:背包大小 w,物品个数 n,每个物品的重量与价值分别对应 w[i] 与 v[i],求放入背包中物品的总价值最大. 动态规划核心:计算并存储小问题的最优解,并将这些最 ...
- 【C/C++】01背包问题/动态规划
按小蓝书上写的大数据情况下没过,按解答区一个大佬的修改了过了 #include <bits/stdc++.h> using namespace std; class Solution { ...
- ACM1881 01背包问题应用
01背包问题动态规划应用 acm1881毕业bg 将必须离开的时间限制看作背包容量,先将他们由小到大排序,然后在排完序的数组中对每个实例都从它的时间限制开始(背包容量)到它的延长时间进行遍历: #in ...
- python实现算法: 多边形游戏 数塔问题 0-1背包问题 快速排序
去年的算法课挂了,本学期要重考,最近要在这方面下点功夫啦! 1.多边形游戏-动态规划 问题描述: 多边形游戏是一个单人玩的游戏,开始时有一个由n个顶点构成的多边形.每个顶点被赋予一个整数值, 每条边被 ...
- 01背包问题(dfs+剪枝)
01背包问题 dfs解法 #include <iostream> #include <cstring> #include <algorithm> #include ...
- 01背包问题(动态规划)python实现
01背包问题(动态规划)python实现 在01背包问题中,在选择是否要把一个物品加到背包中.必须把该物品加进去的子问题的解与不取该物品的子问题的解进行比較,这样的方式形成的问题导致了很多重叠子问题, ...
- 动态规划入门-01背包问题 - poj3624
2017-08-12 18:50:13 writer:pprp 对于最基础的动态规划01背包问题,都花了我好长时间去理解: poj3624是一个最基本的01背包问题: 题意:给你N个物品,给你一个容量 ...
- 动态规划专题 01背包问题详解 HDU 2546 饭卡
我以此题为例,详细分析01背包问题,希望该题能够为大家对01背包问题的理解有所帮助,对这篇博文有什么问题可以向我提问,一同进步^_^ 饭卡 Time Limit: 5000/1000 MS (Java ...
随机推荐
- 【noip模拟】最小点覆盖
Time Limit: 1000ms Memory Limit: 128MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小点覆盖可能有很 ...
- java中阻止类的继承
1.使用final来修饰类 final表示这个类是继承树的末端,不能被继承. 2.将类的构造方法声明为private的,再提供一个static的方法来返回一个类的对象. JAVA语言要求继承时必须在构 ...
- Windows cmd命令反斜杠问题
在Windows的终端输入命令的时候,如果在命令中存在斜杠就无法执行,因为它只支持反斜杠,而在Unix系统中是支持斜杠的,这是两者的区别.所以在Windows终端下执行命令需要注意斜杠问题
- 3Des加解密 C#--->Java
Java的Byte范围为-128至127,c#的Byte范围是0-255.核心是确定Mode和Padding,关于这两个的意思可以搜索3DES算法相关文章. C#采用CBC Mode,PKCS7 Pa ...
- python web开发-flask读取txt文件内容
某些情况下,需要读取flask网站要目录下的txt文件.但是直接在flask网站的目录下创建一个文件是无法访问的.从网站找了一些资料,最终发现通过写一个方法返回txt内容比较简单方便,不过此方法适用于 ...
- Greetings
1,Common English Greetings Hey! Hey man! Hey girl! Hi. How's it going? How are you doing? What's up? ...
- 详解小白利用eclipse+CDT+MinGW搭建C++开发环境
安装JDK.安装Eclipse 2.打开Eclipse 找到“help”下的“Eclipse marketplace” 如图: 3.选择相应的C插件: 因为安装过所以显示installe ...
- 用bat文件启动mongodb
bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cm ...
- docker进阶-利用dcoker Swarm搭建简单集群
什么是Swarm 在介绍Swarm之前我们要说一下什么Docker三剑客? Docker-Machine:负责在多种平台上快速安装 Docker 环境. Docker-Compose:Docker ...
- SpringBoot快速开发REST服务最佳实践
一.为什么选择SpringBoot Spring Boot是由Pivotal团队提供的全新框架,被很多业内资深人士认为是可能改变游戏规则的新项目.早期我们搭建一个SSH或者Spring Web应用,需 ...