问题描述:

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背包问题动态规划解法)的更多相关文章

  1. c语言数据结构:01背包问题-------动态规划

    两天的时间都在学习动态规划:小作业(01背包问题:) 数据结构老师布置的这个小作业还真是让人伤头脑,自己实在想不出来了便去网上寻找讲解,看到一篇不错的文章: http://www.cnblogs.co ...

  2. 0-1背包问题——动态规划求解【Python】

    动态规划求解0-1背包问题: 问题:背包大小 w,物品个数 n,每个物品的重量与价值分别对应 w[i] 与 v[i],求放入背包中物品的总价值最大. 动态规划核心:计算并存储小问题的最优解,并将这些最 ...

  3. 【C/C++】01背包问题/动态规划

    按小蓝书上写的大数据情况下没过,按解答区一个大佬的修改了过了 #include <bits/stdc++.h> using namespace std; class Solution { ...

  4. ACM1881 01背包问题应用

    01背包问题动态规划应用 acm1881毕业bg 将必须离开的时间限制看作背包容量,先将他们由小到大排序,然后在排完序的数组中对每个实例都从它的时间限制开始(背包容量)到它的延长时间进行遍历: #in ...

  5. python实现算法: 多边形游戏 数塔问题 0-1背包问题 快速排序

    去年的算法课挂了,本学期要重考,最近要在这方面下点功夫啦! 1.多边形游戏-动态规划 问题描述: 多边形游戏是一个单人玩的游戏,开始时有一个由n个顶点构成的多边形.每个顶点被赋予一个整数值, 每条边被 ...

  6. 01背包问题(dfs+剪枝)

    01背包问题 dfs解法 #include <iostream> #include <cstring> #include <algorithm> #include ...

  7. 01背包问题(动态规划)python实现

    01背包问题(动态规划)python实现 在01背包问题中,在选择是否要把一个物品加到背包中.必须把该物品加进去的子问题的解与不取该物品的子问题的解进行比較,这样的方式形成的问题导致了很多重叠子问题, ...

  8. 动态规划入门-01背包问题 - poj3624

    2017-08-12 18:50:13 writer:pprp 对于最基础的动态规划01背包问题,都花了我好长时间去理解: poj3624是一个最基本的01背包问题: 题意:给你N个物品,给你一个容量 ...

  9. 动态规划专题 01背包问题详解 HDU 2546 饭卡

    我以此题为例,详细分析01背包问题,希望该题能够为大家对01背包问题的理解有所帮助,对这篇博文有什么问题可以向我提问,一同进步^_^ 饭卡 Time Limit: 5000/1000 MS (Java ...

随机推荐

  1. 8Manage:物流CRM,深度挖掘快递企业下一站蓝海!

    [导读]网购的普及加快了快递物流服务在中国的发展,而物流行业也开始展露出自身巨大的发展潜力和进步空间.其中,作为物流行业根本核心的物流客户关系管理开始引起了管理者的注意,如何升级用户物流服务体验,把握 ...

  2. MSSQL存储过程--CAST和CONVERT使用区别

    数据类型显示转换:CAST和CONVERT(CAST 函数基于 SQL-92 标准并且优先于 CONVERT) ①: CAST是时间类型和字符串之间的转换,使用:CAST(expression AS ...

  3. [转][RabbitMQ+Python入门经典] 兔子和兔子窝

    [转][RabbitMQ+Python入门经典] 兔子和兔子窝 http://blog.csdn.net/linvo/article/details/5750987 RabbitMQ作为一个工业级的消 ...

  4. 1111 WordReplace

    #include<iostream> #include<string> using namespace std; int main() { string sa,sb,s; wh ...

  5. openstack的最简单安装

    环境:采用centos7.4,最低4g内存.大硬盘.内存小了会在安装过程中报错.整个过程大概2小时,看网速快慢,所有的节点和服务都安装在同一台机器上.   yum update -y yum inst ...

  6. Beego 框架学习(一)

    Beego官网本身已经整理的非常详细了,但是作为一个学习者,我还是决定自己好好整理一下,这样在后面使用的时候自己对每部分才能非常熟悉,及时忘记了,也可以迅速定位自己要用的知识在哪里.当然也是对官网的一 ...

  7. VMware 滴滴声解决

    去除虚拟机tab时的滴滴声 # cat /etc/inputrc Set bell-style none 重启生效

  8. 13.HashMap TreeMap HashTable LinkedHashMap 的区别

    数据库基本连接equals和hashCode详解 http://www.cnblogs.com/XMMDMW/p/6502355.html

  9. 一些常见的shell命令和git命令

    shell命令: pwd : (Print Working Directory) 查看当前目录 cd (Change Directory) 切换目录,如 cd /etc ./当前目录  ../上级目录 ...

  10. mac安装tensorflow

    tensorflow简介 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着基于数 ...