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部分.语言是 ...
随机推荐
- digitalpersona 开发(系统托盘,监听指纹扫描)
其实很简单,主要是生成 DPFPCapture 对象时,设置他的优先级就可以了. (改成High的话,发布后,windows系统会认为你是病毒.....) C# //设置优先级,这个就是系统托盘后 ...
- JPA Query in 集合
使用 :param的方式来传递参数,下面举个例子 @PersistenceContext EntityManager em @Override public List<Map> ...
- IdentityServer_0_参考资料
官方 项目地址:https://github.com/IdentityServer/IdentityServer4 官方Demo:https://github.com/IdentityServer/I ...
- hdu6089 Rikka with Terrorist
题意:n*m的平面内有K个不安全点,Q个询问位置在(x,y)的人能走到多少个点?走到:(x,y)和(x',y')之间的矩形中不包含不安全点. 标程: #include<bits/stdc++.h ...
- CF982F The Meeting Place Cannot Be Changed
题意:给你一张有向图,某人会任意选择起点然后走无穷多步,问是否存在一个点(要求输出)不管他起点在何处怎么走都必经?n<=100005,m<=500005. 标程: #include< ...
- 异或+桶——cf768C
有个结论是到最后肯定出现循环节..感觉这种做法有点歪 正解当然是题解啦 虽然到了1e8,但是cf上还是能过的 #include<bits/stdc++.h> #define rep(i,s ...
- DELPHI实现类似仿360桌面的程序界面
1.窗体半透明: Alphablend属性为true;Alphablendvalue的值为100 2.窗体透明: formCreate: Self.TransparentColor := True;S ...
- HttpClient 使用案例
package com.qifeng.config.ygx.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fas ...
- day25-静态、组合、继承
#!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...
- Collection单列集合中的常用实现类
Collection 集合层次的根接口 List 有序 有索引 可以重复 ArrayList 底层数据结构是数组 查询快 增删快 线程不安全 效率高 LinkedList 底层数据结构是链表 查询慢 ...