PAT (Advanced Level) 1068. Find More Coins (30) DFS+剪枝 或 动态规划
2015-07-28 20:30
573 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
上网看了一下,说是背包问题,可以用动态规划。研究研究,等会再来更新。[/code]
[/code]
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
用DFS+剪枝,30分可以得29分,最后一个case超时。
/*2015.7.28cyq*/ #include <iostream> #include <vector> #include <algorithm> #include <fstream> using namespace std; //ifstream fin("case1.txt"); //#define cin fin //DFS+剪枝 30分可以得29分,最后一个case超时 void dfs(const vector<int> &a,int target,int start,int end,vector<int> &path,vector<int> &result){ if(!result.empty()) return ; if(target==0){ result=path; return ; } for(int i=start;i<=end;i++){ if(target<a[i]) return;//剪枝 path.push_back(a[i]); dfs(a,target-a[i],i+1,end,path,result); path.pop_back(); } } int main(){ int N,M; scanf("%d%d",&N,&M); vector<int> a(N); for(int i=0;i<N;i++) scanf("%d",&a[i]); sort(a.begin(),a.end()); vector<int> path,result; dfs(a,M,0,N-1,path,result); if(result.empty()) printf("No Solution"); else{ int len=result.size(); printf("%d",result[0]); for(int i=1;i<len;i++) printf(" %d",result[i]); } return 0; }
上网看了一下,说是背包问题,可以用动态规划。研究研究,等会再来更新。[/code]
来了,我是分割线*************************************************************
动态规划:
d[i][j]=true表示前i个硬币能够选出若干个达到价值j。 关键等式为d[i][j]=d[i-1][j]||d[i-1][j-coin[i]],分别为不用coin[i]和用coin[i]两种方案。 迭代过程中用used[i][j]=ture表示在d[i][j]==true时存在可以用coin[i]的方案。 由于结果要求优先选小面值硬币,先将硬币从大到小排序,从大面值硬币开始处理,迭代到最后的小面值。越小的硬币越晚参与处理,能参与到更多方案中。 从小面值开始,根据used数组进行回溯可得出结果。提交代码后AC。
/*2015.7.28cyq*/ #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int N,M; scanf("%d%d",&N,&M); vector<int> coin(N+1); for(int i=1;i<=N;i++) scanf("%d",&coin[i]); sort(coin.begin()+1,coin.end(),greater<int>()); vector<vector<bool> > d(N+1,vector<bool>(M+1,false)); vector<vector<bool> > used(N+1,vector<bool>(M+1,false)); for(int i=0;i<=N;i++) d[i][0]=true;//用前i个硬币显然能组合出总价值0 for(int i=1;i<=N;i++){ for(int j=1;j<=M;j++){ if(j-coin[i]>=0&&d[i-1][j-coin[i]]){//有方案能用上当前硬币i d[i][j]=true; used[i][j]=true; }else{ d[i][j]=d[i-1][j];//当前硬币不能用 } } } if(!d [M]) printf("No Solution\n"); else{ vector<int> result; int i=N,j=M; while(j){ while(!used[i][j]) i--; result.push_back(coin[i]); j-=coin[i]; i--; } int len=result.size(); printf("%d",result[0]); for(int i=1;i<len;i++) printf(" %d",result[i]); } return 0; }
[/code]
相关文章推荐
- Java学习之路0728(十二)(Class类、反射、Annotation功能注解)
- HDOJ 1896 Stones(优先队列)
- APP首次启动引导界面和启动界面设置——iOS开发
- 【面试加分项】java异常处理之try_catch_finally
- IOS被遗忘的知识
- OC中 static 与 const 的作用
- Activity里添加ViewGroup ViewGroup里添加ViewGroup
- tinyxml使用笔记与总结 http://www.cppblog.com/elva/archive/2008/04/24/47907.html
- hdu 5319 Painter
- Reorder List
- 判断单链表是否有环以及环的连接点
- 百练2757最长上升子序列
- IndexError: list index out of range的错误原因
- JSP概述
- 渣渣ACM日记——3461-Oulipo(POJ)
- VS中使用TinyXml操作xml文件
- Android ViewPager引导页(三)
- prim最小生成树算法题poj2485
- OpenGL关于glBegin的参数的理解
- I/O复用中,epoll 和select 的区别,以及epoll和select的实例