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 ...
随机推荐
- EasyDSS流媒体解决方案实现的RTMP/HLS视频直播、直播鉴权(如何完美将EasyDSS过渡到新版)
上一篇博文介绍了EasyDSS点播功能,然后作为RTMP流媒体服务器,接受RTMP推流.进行实时的直播流分发又是自身一大核心功能. 需求背景: 写本篇博文的一个目的是向大家介绍一下EasyDSS新版的 ...
- 【HTML5开发系列】meta元素详解
meta元素可以用来定义文档的各种元数据.他有很多种用法,一个HTML文档可以包含多个meta元素. meta元素在HTML5中的变化 charset属性是HTML5中新增的.在HTML4中,http ...
- JAVA NIO 简介 (netty源码死磕1.1)
[基础篇]netty 源码死磕1.1: JAVA NIO简介 1. JAVA NIO简介 Java 中 New I/O类库 是由 Java 1.4 引进的异步 IO.由于之前老的I/O类库是阻塞I/ ...
- php的json_encode不兼容JSON_UNESCAPED_UNICODE
//php的json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案 function _json_encode($value) { if (version_compare( ...
- c#的const可以用于引用类型吗
答案是可以的.不过用const修饰的类实例只能是null. class A{ public int a=0; } class B{ const A constA=null; const object ...
- DHTMLTree、Dtree和Ztree的学习使用
一.DHTMLTree是树菜单,允许我们快速开发界面优美,基于Ajax的javascript库.她允许在线编辑,拖拽,三种状态(全选.不选.半选),复选框等模式.同时在加载大数据量的时候,仍然可以保持 ...
- 运用starling开发的手游FlappyBird
最近想向游戏方面发展,于是用starling做了一个简易版的FlappyBird,纯AS3开发,权当是技术学习.在发布之后才明白要发布一个没有版权的app是有多困难,审核了N遍之后终于通过审核,下面发 ...
- 工作中你肯定会有关于 Yii2 的小贴士用法,在下面评论分享出来吧。
场景: 数据库有user表有个avatar_path字段用来保存用户头像路径 需求: 头像url需要通过域名http://b.com/作为基本url 目标: 提高代码复用 此处http://b.com ...
- Python 3 udp 套接字
Python 3 udp套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号, ...
- php 通用redis类
php 通用redis类 <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止 ...