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 ...
随机推荐
- JetBrains Rider 在 Mac 环境下将 cs 文件生成 exe
因为自己的开发环境是 Mac + Rider 组合,想测试网络编程相关内容.想在Windows 虚拟机上运行一套代码来与Mac 机进行测试,但又不想在虚拟机上安装一套开发环境.最终找到的解决方案是通过 ...
- day06.1-磁盘管理
1. 添加磁盘 打开虚拟机,依次点击"编辑虚拟机设置" |—> "添加" |—> "硬盘" |—> "选择硬盘类 ...
- Spring注解实现定时功能以及Quartz定时器
一:Spring注解实现--------->Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz 1:maven配置: <!-- quartz--> <d ...
- 新建maven 父子模块项目
第一步: 第二步: 先创建个简单的空架结构作为父项目 第三步: 创建子项目 第四步: 切换显示不同的maven子项目显示方式 关于maven中的parent聚合一直都有没好好总结,固有这篇. ---- ...
- window下安装git与git使用
有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...
- Luogu P2107 小Z的AK计划 堆贪心
好久不做这种题了... 存一下每个点的位置和时间,由于达到某个位置跟之前去哪里AK的无关,所以在时间超限后,可以用大根堆弹掉之前消耗时间最大的,来更新答案,相当于去掉之前花费最大的,直到时间不在超限. ...
- Codeforces Round #316 (Div. 2) A
Description The country of Byalechinsk is running elections involving n candidates. The country cons ...
- 自定义Razor 标签
1.首先需要一个abstract class WebViewPage<T> ,继承系统的 System.Web.Mvc.WebViewPage<TModel> 再定义一个Web ...
- stark组件之创建
stark组件之需求 仿照Django中的admin , 开发了自己的stark组件,实现类似数据库客户端的功能,对数据进行增删改查 . stark之创建 1.在项目中 创建stark应用,app01 ...
- mariaDB 远程连接不上
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; // %:表示从任何主机连接 ...