1068. Find More Coins (30)
来源:互联网 发布:java regex 编辑:程序博客网 时间:2024/06/04 18:52
- Find More Coins (30)
时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
第一次学习背包问题的算法,使用动态规划结题。
设F(N,M)为前N个硬币,能够拼成的不大于M的最大数
需要用到一个公式
F(i,j)=MAX{F[i][j],F[i-1][j-c[i]]+c[i]}
代表每个硬币都有放入和不放入两种情况,其中c[i]为第i枚硬币的数额。
①放入第i个硬币,即f(i-1,j - c[i]) + c[i]。
②不放入第i个硬币,即f(i-1,j)。
注意要对c[i]从大到小排序,从数额大的开始放。
然后在建立一个has[i][j]代表着,总价格为j时,是否有用到第i枚硬币。
最后若f[n][m]==m,即n枚硬币能组成价值为m的情况,说明有解。
再从小到大,依次入队,然后输出。
我们都曾拥有最美的时光 的文章,感谢他的分享,我几乎是照搬他的代码,自己加了点注释。
#include <iostream>#include <algorithm>#include <vector>#include<string.h>using namespace std;#define MAXTOTAL 10001#define MAXAMOUNT 101int f[MAXTOTAL][MAXAMOUNT]={0}; //f[n][m]表示 前n个数中 得出的 最接近m 的值bool has[MAXTOTAL][MAXAMOUNT]={false}; //has[n][m]表示在前n个数中得出最接近m的值时 是否用到c[n]vector<int>c ;//存储硬币int calcClosestSum(int n,int m){ int i,j; int sec;// 表示放入c[i]后的值 for(i=1;i<n+1;i++) { for(j=1;j<=m;j++) { if(j-c[i]<0) sec=0;// j为当前最大值,如果c[i]大于j,则说明放入后是非法值,可以设其为0,表示面值无效。 else sec=f[i-1][j-c[i]]+c[i];// 正常情况下计算放入c[i]的值。 if(f[i-1][j]>sec) //去掉第i枚硬币后,剩下硬币的最大值大于sec,则不放入第i枚硬币 { f[i][j]=f[i-1][j]; } else //放入第i枚硬币 { f[i][j]=sec; has[i][j]=true;//用到c[i]了 设has[i][j]为true } } } return f[n][m];}bool cmp(const int& A,const int& B){ return A>B;}int main(){ freopen("in.txt","r",stdin); int n,m; cin>>n>>m; c = vector<int>(n+2,0); int i; for(i=0;i<n;i++) { cin>>c[i+1]; } sort(&c[1],&c[n+1],cmp); //从大到小排序 int res = calcClosestSum(n,m); if(res==m) //有解 { vector<int> v; while(m) { while(!has[n][m]) //从最小的数开始找,找到后入栈 n--; v.push_back(c[n]); m = m - c[n]; n--; } for(i=0;i<v.size()-1;i++) cout<<v[i]<<' '; cout<<v[i]<<endl; }else //无解 cout<<"No Solution"<<endl; system("PAUSE"); 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)
- 京东DNN实验室:大数据、深度学习与计算平台的实践
- 第2周实践项目3--体验复杂度(2)汉诺塔
- Oracle_使用bat命令执行sql命令文件
- IOS之数据持久化(归档与反归档)
- UGUI Button控件
- 1068. Find More Coins (30)
- diff
- C++中引用(&)的用法和应用实例
- 1090. Highest Price in Supply Chain (25)
- 15/9/9/Service发送倒计时广播Receiver显示
- Mac 下安装配置Mysql
- 大二新学期的开始
- java学习日记4
- JAVA异常处理机制