问题描述:

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. 设计模式——抽象工厂模式(C++实现)

    #include <iostream> #include <string> using namespace std; class STAbstractProductA { pu ...

  2. Loadrunner 11在win7录制中失败“the recording of the application was terminated by windows due to window data execution prevention feature"

    在录制过程中遇到如下问题: 查了下如何Enable, Disable Data Execution Prevention (DEP) in Windows 10/8/7 (http://www.the ...

  3. guava cache使用和源码分析

    guava cache的优点和使用场景,用来判断业务中是否适合使用此缓存 介绍常用的方法,并给出示例,作为使用的参考 深入解读源码. guava简介 guava cache是一个本地缓存.有以下优点: ...

  4. Linux如此“自私”?

    Linux如此“自私”? “如果当时我真的知道从头建立一个操作系统的难度,肯定是不会有勇气去做的.”1991年8月25日,随着林纳斯·托瓦兹(Linus Torvalds)这句“天真”的描述,Linu ...

  5. ACM搜索问题盘点

    深度搜索:棋盘问题,详见http://poj.org/problem?id=1321 //#include<bits/stdc++.h> #include<cstdio> #i ...

  6. React 组件间通讯

    React 组件间通讯 说 React 组件间通讯之前,我们先来讨论一下 React 组件究竟有多少种层级间的关系.假设我们开发的项目是一个纯 React 的项目,那我们项目应该有如下类似的关系: 父 ...

  7. pat 喊山

    L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出" ...

  8. Item 15: 只要有可能,就使用constexpr

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果说C++11中有什么新东西能拿"最佳困惑奖" ...

  9. springmvc与freemarker的整合

    官方简介:FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具. 它不是面向最终用户的,而是一个Java ...

  10. java程序在没有java环境的电脑上执行的方法(关键词jar,exe)

    可以让你写的java程序在别人没有任何java配置以及环境的情况下执行 写好程序 在程序对应的package上右键->export->java->Runnable JAR file- ...