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 ...
随机推荐
- php之 人员的权限管理
1.想好权限管理的作用? 2.有什么权限内容? 3.既然有权限管理那么就会有管理员? 4.登录后每个人员的界面会是不一样的? 一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户 ...
- share(发包方面)
share(发包方面) 接收所有map发过来的包,这个是GS线程驱动的 { for (;;) { //... if(!itMap.second->RecvData(Pkt)) break; if ...
- BI测试
BI概念: 商业智能(Business Intelligence 简称BI),指数据仓库相关技术与应用的通称.指利用各种智能技术,来提升企业的商业竞争力.是帮助企业更好地利用数据提高决策质量的技术,包 ...
- 九度OJ 1354:和为S的连续正数序列 (整除)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2028 解决:630 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不 ...
- iOS解决导航引起视图高度问题
经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似) 1.通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的) ...
- CSS 布局实例系列(一)总结CSS居中的多种方法
使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...
- 【Android】开源项目汇总
Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progres ...
- 蓝屏代码stop:0X000000EA(0X85E286B8,0X8635F210,0XF7A53CBC,0X00000001)
你这是显卡驱动问题,我把蓝屏代码都给你,以后在出现蓝屏自己看看行了. 1.0x0000000A:IRQL_NOT_LESS_OR_EQUAL ◆错误分析:主要是由问题的驱动程序.有缺陷或不兼容的硬件与 ...
- Windows存储管理之磁盘结构详解
Windows磁盘结构: Windows的主流磁盘结构分为MBR和GPT两种.MBR是早期Windows的唯一选择,但是随着物理磁盘的容量不断增大.GPT结构成为目前的主流,最大支持超过2TB的容量, ...
- JSON JsonArray和JsonObject学习资料
资料地址: http://www.json.org/json-zh.html