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 Solution
dfs加剪枝,先整体排序,把比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 分)的更多相关文章

  1. 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 ...

  2. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  3. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

  4. 1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  5. 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:表示每次图像尺寸减小 ...

  6. 1048 Find Coins (25 分)

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00

    C#  时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...

  8. 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 ...

  9. 1068 Find More Coins (30分)(dp)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

随机推荐

  1. github-readme.md-格式

    大标题 大标题一般显示project名,类似html的<h1> 你仅仅要在标题以下跟上=====就可以 中标题 中标题一般显示重点项,类似html的<h2> 你仅仅要在标题以下 ...

  2. 批量删除redis某个键值

    /usr/local/redis/src/redis-cli -h 172.28.6.23  -p 6383 keys "virtual*" |xargs /usr/local/r ...

  3. Hive高级

    HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...

  4. 九度OJ 1196:成绩排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4339 解决:1476 题目描述: 用一维数组存储学号和成绩,然后,按成绩排序输出. 输入: 输入第一行包括一个整数N(1<=N< ...

  5. springboot + swagger2 学习笔记

    引入步骤 1.添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springf ...

  6. [BJWC2012]冻结

    [BJWC2012]冻结 luogu BZOJ 分层图最短路,层与层之间连半边权边 #include<bits/stdc++.h> using namespace std; const i ...

  7. template.helper 检测浏览器 时间转换

    template.helper('changeTime',function getLocalTime(nS) { var b=nS.substr(6,13); var c=parseInt(b) va ...

  8. 【WEB】高并发Web服务的演变-节约系统内存和CPU

    目前主流浏览器通常可以存在2-6个并发. 连接和请求,占据了服务器的大量CPU和内存等资源.在资源数目超过100+的网站页面中,使用更多的下载连接,非常有必要. 缓解“高并发”的压力的手段. 一. W ...

  9. static_func

    <?php function testing() { static $a = 1; $a *= 2; echo $a."\n"; } testing(); testing() ...

  10. js 动态加载事件的几种方法总结

    本篇文章主要是对js 动态加载事件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助   有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添 ...