1068. Find More Coins (30)
来源:互联网 发布:网络高清解码器 编辑:程序博客网 时间:2024/06/05 21:08
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 95 9 8 7 2 3 4 1Sample Output 1:
1 3 5Sample Input 2:
4 87 2 4 3Sample Output 2:
No Solution
动态规划+回溯
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;int n,m;int v[10005];int w[10005][105];int dp[10005];int op[10005];int index1=0;int ii,jj;bool cmp(int a,int b){ return a>b;}int main(){ cin>>n>>m; for (int i=1;i<=n;i++) cin>>v[i]; sort(v+1,v+n+1,cmp); for (int i=1;i<=n;i++) for (int j=m;j>=v[i];j--) { if (dp[j-v[i]]+v[i]>=dp[j]) { dp[j]=dp[j-v[i]]+v[i]; w[i][j]=1;//记录在总数为j时,选择了第i个面值。 } } if (dp[m]!=m) { printf("No Solution"); return 0; } ii=n;jj=m;//回溯 while(ii>0) { if (w[ii][jj]==1) { op[index1++]=v[ii]; jj-=v[ii]; } ii--; } sort(op,op+index1); printf("%d",op[0]); for ( ii=1;ii<index1;ii++) printf(" %d",op[ii]); return 0;}
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- hdu 6034 2017 Multi-University Training Contest
- 04.安装eclipse及汉化
- 线段树,区间开方(Rikka with Sequence,HDU 5828)
- 10129
- [GNU/Linux] Linux系统调用-线程相关(二):线程同步
- 1068. Find More Coins (30)
- [POJ]2406Power Strings KMP
- Python爬虫学习纪要(一):BeautifulSoup相关知识点
- 浅谈C++STL中的优先队列
- 两阶段提交2PC
- 搜索专题: HDU1016Prime Ring Problem
- JS判断一个对象是否为document.Element
- (2) 数据结构的起源--每天五分钟学习数据结构
- LeetCode 283 Move Zeroes