PAT 甲级 1068 Find More Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976
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 1coins 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 <bits/stdc++.h>
using namespace std; const int maxn = 1e4 + 10;
int N, M;
int num[maxn], dp[maxn];
bool vis[maxn][110]; int cmp(int a, int b) {
return a > b;
} int main() {
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i ++)
scanf("%d", &num[i]);
sort(num + 1, num + 1 + N, cmp); for(int i = 1; i <= N; i ++) {
for(int j = M; j >= num[i]; j --) {
if(dp[j] <= dp[j - num[i]] + num[i]) {
vis[i][j] = true;
dp[j] = dp[j - num[i]] + num[i];
}
}
}
if(dp[M] != M) printf("No Solution");
else {
vector<int> ans;
int sum = M, st = N;
while(sum > 0) {
if(vis[st][sum]) {
ans.push_back(num[st]);
sum -= num[st];
}
st --;
} for(int i = 0; i < ans.size(); i ++) {
if(i != 0) printf(" ");
printf("%d", ans[i]);
}
}
return 0;
}
这个居然是个 01 背包 没看出来 哭唧唧
PAT 甲级 1068 Find More Coins的更多相关文章
- 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甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- PAT甲级——A1068 Find More Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 【PAT甲级】1048 Find Coins (25 分)(二分)
题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- 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 ...
随机推荐
- Scala学习之路 (五)Scala的关键字Lazy
Scala中使用关键字lazy来定义惰性变量,实现延迟加载(懒加载). 惰性变量只能是不可变变量,并且只有在调用惰性变量时,才会去实例化这个变量. 在Java中,要实现延迟加载(懒加载),需要自己手动 ...
- 随手练——ZOJ 1093 Monkey and Banana(动态规划)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=93 一堆科学家研究猩猩的智商,给他M种长方体,每种N个. 然后,将一个 ...
- IIS7.5全站301跳转,内页+带参数url,这才是真正的全站跳转
说好的转型安全领域,可是我还是忍不住要给大家分享这个教程.因为这个问题很常见,大部分人都遇到了(可能你没注意),困扰了我很久,相信这是一篇真正适合你的IIS301跳转教程. 背景 说到301跳转,作为 ...
- canal数据同步目录
我们公司对于数据同步有以下需求 1.多个mysql库中有一些基础表需要数据统一,mysql跨库同步 2.mysql热数据加载到redis 3.全文检索需要mysql同步到es 4.数据变更是附属的其它 ...
- WorldWind源码剖析系列:可渲染对象列表类RenderableObjectList
可渲染对象列表类RenderableObjectList用来存储与一个星球体相关联的全部的可渲染对象,继承自可渲染对象类RenderableObject.RenderableObjectList的类图 ...
- clock gating check
在 sta 分析时,经常会碰到 clock gating cell (一般是 ICG cell 或者 latch)引起的 violation,这种 violation 很常见,而且往往很难修. 为什么 ...
- js中数组的使用
使用js时候,很多情况下要使用数组.就写写数组中一些常用的方法. js中定义一个数组,一般有以下2种方法. 1. var arr = new Array(3); // 声明数组有3个元素 arr[0] ...
- 2《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——操作文件
已经学习了基本的命令,现在是时候学习操作文件了,这也是命令行的重要任务.还是基于本教程的事先规定,本教程是入门级的,不要求熟悉类似编辑文本的程序(这些文本编辑程序,将在下个系列教程中介绍, Learn ...
- 2017-2018-2 20155315《网络对抗技术》Exp8 :Web基础
实验目的 理解HTML,学会Web前端.Web后端和数据库编程及SQL注入.XSS攻击测试 教程 实验内容 操作程序规律 运行脚本或可执行文件 查看配置文件 出错找日志 Web前端HTML 能正常安装 ...
- 20155321 《网络攻防》 Exp5 MSF基础应用
20155321 <网络攻防> Exp5 MSF基础应用 基础问题 用自己的话解释什么是exploit,payload,encode 关于exploit,我觉得exploit是利用一些工具 ...