pat1068. Find More Coins (30)
1068. Find More Coins (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 104 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 (<=104, the total number of coins) and M(<=102, 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
学习网址:http://blog.csdn.net/xyt8023y/article/details/47255241
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int mo[],f[][];
bool vis[][];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int i,j,n,m;
scanf("%d %d",&n,&m);
for(i=; i<=n; i++)
{
scanf("%d",&mo[i]);
}
sort(mo+,mo+n+,cmp);
//f[i][j]=maz(f[i-1][j],f[i-1][j-mo[i]]+mo[i])
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
if(mo[i]<=j&&(f[i-][j-mo[i]]+mo[i]>=f[i-][j]))//这里要取到等号,为了状态的衔接
{
f[i][j]=f[i-][j-mo[i]]+mo[i];
vis[i][j]=true;
}
else
{
f[i][j]=f[i-][j];
}
}
}
/*for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
}
cout<<endl;
}*/ if(f[n][m]!=m)
{
printf("No Solution\n");
}
else
{
vector<int> v;
while(m)
{
while(!vis[n][m])
{
n--;
}
v.push_back(mo[n]);
m-=mo[n];
n--;
}
printf("%d",v[]);
for(i=;i<v.size();i++){
printf(" %d",v[i]);
}
printf("\n");
}
return ;
}
pat1068. Find More Coins (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 ...
- 1068. Find More Coins (30)
题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- 1068 Find More Coins (30)(30 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- 1068 Find More Coins (30分)(dp)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...
- 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)
题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...
- 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 ...
随机推荐
- SQL的发展史
在20世纪60年代,网状数据库系统(如CODASYL)和分层数据库系统(如IMS TM)是用于自动化银行业务.记帐和订单处理系统的一流技术,这些系统是由于商业大型计算机的引入才启用的.而SQL是在70 ...
- Memcached Cache
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached ...
- asp.net core 之多语言国际化自定义资源文件
先说说 asp.net core 默认的多语言和国际化. 官方文档 一:基本使用方法 先要安装 包 Microsoft.AspNetCore.Mvc.Localization (依赖 Microsof ...
- 扒一扒spring,dom4j实现模拟实现读取xml
今天leadr提出需求,原来公司项目中读取解析xml文件的代码效率太低,考虑切换一种xml为数据封装格式与读取方式以提高效率.我这灵机一动spring对bean的依赖注入就是读取xml文件,可以尝试扒 ...
- epoll简介
1.epoll简介 epoll是I/O事件通知工具,与select/poll相比,epoll最大的好处在于它不会随着监听fd数目的增长而效率降低.epoll API既可以用作edge触发的接口,也可以 ...
- gRPC官方文档(异步基础: C++)
文章来自gRPC 官方文档中文版 异步基础: C++ 本教程介绍如何使用 C++ 的 gRPC 异步/非阻塞 API 去实现简单的服务器和客户端.假设你已经熟悉实现同步 gRPC 代码,如gRPC 基 ...
- eclipse - 链接hadoop
插件: 配置:Map/Reduce Location 问题:An internal error occurred during: "Map/Reduce location status up ...
- PHP 符号
注解符号: // 单行注解 /* */ 多行注解 引号的使用 ’ ’ 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,PHP动态处理然后输出,一般 ...
- Oracle数据稠化
姓名 学科 分数 城市 张三 ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...