1068 Find More Coins (30)(30 分)
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 10^4^ 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 (<=10^4^, the total number of coins) and M(<=10^2^, 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 V~1~ <= V~2~ <= ... <= V~k~ such that V~1~ + V~2~ + ... + V~k~ = 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 Solutiondfs加剪枝,先整体排序,把比m大的去掉,如果加到一个数比m大了就返回,每一步同一个数只算一次,比如说到了i步,选了第j个数,继续dfs,当返回到这里时,继续尝试i+1,如果i+1和i相同直接跳,因为在后边已经测试过这种情况了。
也可以一开始就计算总和,如果总的和小于m就不用dfs了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
int n,m;
int s[],ans[];
int flag;
void dfs(int k,int last,int sum) {
if(flag)return;
if(sum > m)return;
if(sum == m) {
flag = k;
return;
}
for(int i = last;i < n;i ++) {
if(flag)return;
if(i != last && s[i] == s[i - ])continue;
ans[k] = s[i];
dfs(k + ,i + ,sum + s[i]);
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < n;i ++) {
scanf("%d",&s[i]);
}
sort(s,s + n);
while(s[n - ] > m)n --;
dfs(,,);
if(!flag)printf("No Solution\n");
else {
printf("%d",ans[]);
for(int i = ;i < flag;i ++) {
printf(" %d",ans[i]);
}
}
}
1068 Find More Coins (30)(30 分)的更多相关文章
- 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[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- 1068 Find More Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))
# 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00
C# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...
- java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...
- 1068 Find More Coins (30分)(dp)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
随机推荐
- c/c++一些小知识点(特此总结)
---恢复内容开始--- ---恢复内容结束---
- 【HTML5开发系列】meta元素详解
meta元素可以用来定义文档的各种元数据.他有很多种用法,一个HTML文档可以包含多个meta元素. meta元素在HTML5中的变化 charset属性是HTML5中新增的.在HTML4中,http ...
- hdu 4068 I-number【大数】
题目: http://acm.hdu.edu.cn/showproblem.php?pid=4608 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 【python】-- 类的装饰器方法、特殊成员方法
装饰器方法 类的另外的特性,装饰器方法:静态方法(staticmethod).类方法(classmethod).属性方法(property) 一.静态方法 在方法名前加上@staticmethod装饰 ...
- Java & 混型
1.C++ & 混型 C++能够记住其模板参数的类型,对于混型来说,使用参数化类型更加优雅. #include <string> #include <ctime> #i ...
- linux c编程:FIFO
前面介绍的pipe属于匿名管道 管道的主要局限性正体现在它的特点上: 只支持单向数据流: 只能用于具有亲缘关系的进程之间: 没有名字: 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时,为缓冲区 ...
- pinpoint改造支持查询
原架构 改造后架构
- MVC中不能使用ViewBag
在工程文件中添加 <Reference Include="Microsoft.CSharp" /> <Reference Include="System ...
- flex DataGroup
示例1: <?xml version="1.0" encoding="utf-8"?> <s:DataGroup xmlns:fx=" ...
- day6 装饰器总结
装饰器:开放封闭原则,为一个函数加上新的功能,不改变原函数,不改变调用方式 def fun2(wtf): def fun3(): print('i am pythoner!!! ') wtf() re ...