PAT甲级——A1068 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M;
int coins[], dp[];
bool visit[][];
int main()
{
cin >> N >> M;
for (int i = ; i <= N; ++i)
cin >> coins[i];
sort(coins + , coins + N + , [](int a, int b) {return a > b; });
for (int i = ; i <= N; ++i)
{
for (int j = M; j >= coins[i]; --j)//目标递减
{
if (dp[j] <= dp[j - coins[i]] + coins[i])
{
dp[j] = dp[j - coins[i]] + coins[i];
visit[i][j] = true;//取
}
}
}
if (dp[M] != M)
cout << "No Solution" << endl;
else
{
vector<int>res;
int v = M, index = N;
while (v > )
{
if (visit[index][v] == true)
{
res.push_back(coins[index]);
v -= coins[index];
}
--index;
}
for (int i = ; i < res.size(); ++i)
cout << res[i] << (i == res.size() - ? "" : " ");
}
return ;
}
PAT甲级——A1068 Find More Coins的更多相关文章
- PAT甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- PAT 甲级 1068 Find More Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...
- 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 ...
- 【PAT甲级】1048 Find Coins (25 分)(二分)
题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
随机推荐
- 自动化测试之sikuli调研
调研结果 Sikuli可用于web和app的自动化测试中,操作简单,代码容易,但截图过程太过繁琐,所需要的图片内存占用量大,且sikuli的图片识别度较低,需对所要操作的图片进行精准截图. 什么是Si ...
- CF538G Berserk Robot
题意:一个机器人在一个无穷大的网格图中,每秒能够上下左右走一步.它的行走方向序列是长度为l的循环.给你n个线索,包括ti:时间,xi,yi走到的坐标.让你构造出行走的方向序列? 标程: #includ ...
- day24 模块
Python之路,Day12 = Python基础12 模块 本质为py程序 分类: 内置模块 time time.time() ---> 当前时间的时间戳:浮点型 time.localtim ...
- CF 1063B Labyrinth
传送门 解题思路 看上去很简单,\(bfs\)写了一发被\(fst\)...后来才知道好像一群人都被\(fst\)了,这道题好像那些每个点只经过一次的传统\(bfs\)都能被叉,只需要构造出一个一块一 ...
- [CTSC2018]青蕈领主
[CTSC2018]青蕈领主 题解 首先,连续段要知道结论: 连续段要么不交,要么包含 所以是一棵树!每个位置的father是后面第一个包含它的 树形DP! 设dp[x],x为根的子树,(设管辖的区间 ...
- C#,判断数字集合是否是连续的
/// <summary> /// 判断数字集合是否是连续的 /// </summary> /// <returns></returns> public ...
- centos7 将home目录空间扩容到根目录
[root@localhost ~]# umount /home/ [root@localhost ~]# lvremove /dev/mapper/centos-home Do you really ...
- PAT甲级——A1135 Is It A Red-Black Tree 【30】
There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...
- 万恶之源-python介绍
PATH OF PYTHON (生命短暂,我要学pythonヾ(◍°∇°◍)ノ゙) 一.Python介绍: 简史:Python诞生于1989年的圣诞节, 创始人为Guido van Rossum, 又 ...
- git工作区和暂存区图