1068. Find More Coins (30)
2015-09-06 14:21
281 查看
题目链接:http://www.patest.cn/contests/pat-a-practise/1068
题目:
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:
分析:
刚开始以为是DFS,做出来但是超时了。
得用DP动态规划做
AC代码:
显示DFS:
input info
4 9
1 3 4 5
dp[i][j] info
0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 1
0 0 1 1 1 0 1 1 1
1 0 1 1 1 1 1 1 1
pre[i][j] info
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 1 1 0
1 0 0 1 1 1 0 1 1
result info
1 3 5
截图:
DFS的,有一个超时了:
——Apie陈小旭
题目:
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
分析:
刚开始以为是DFS,做出来但是超时了。
得用DP动态规划做
AC代码:
显示DFS:
#include<stdio.h> #include<algorithm> #include<vector> using namespace std; vector<int>ans; bool success = false; int N, M; // int *price = new int ; int price[10001]; void DFS(int idx,int sum){ for (int i = idx; i < N; i++){ int curPrice = price[i]; if (sum + curPrice < M){ ans.push_back(curPrice); DFS(i + 1, sum + curPrice); if (success)return; ans.pop_back(); } else if (sum + curPrice == M){ ans.push_back(curPrice); success = true; return; } else break; } } int main(){ freopen("F://Temp/input.txt", "r", stdin); scanf("%d%d", &N, &M); for (int i = 0; i < N; i++){ scanf("%d", (price + i)); } sort(price, price + N); ans.clear(); DFS(0,0); if (success){ for (int i = 0; i < ans.size(); i++){ if (i == ans.size() - 1){ printf("%d\n", ans[i]); } else printf("%d ", ans[i]); } } else printf("No Solution\n"); return 0; }后来参考了网上的资料:http://blog.csdn.net/sup_heaven/article/details/20133423
#include<stdio.h> #include<algorithm> #include<stdlib.h> using namespace std; int price[10001]; int ans[10001]; int dp[10001][101];//用dp[i][j]记录当使用前i个硬币时是否可以达到价值j,可以则为1,反之为0; int pre[10001][101];//用pre[i][j]记录当前第i个硬币是否在状态dp[i][j]中使用,是则为1,反之为0; bool cmp(int a, int b){ return a > b; } int main(){ freopen("F://Temp/input.txt", "r", stdin); int N, M; scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++){ scanf("%d", (price + i)); } sort(price + 1, price + N + 1, cmp);//先对数据进行从大到小排序,保证最后的输出是smaller的,用下面的例子能更好的说明算法的思想。 for (int i = 0; i <= N; i++){ for (int j = 0; j <= M; j++){ dp[i][j] = 0; pre[i][j] = 0; } } for (int i = 0; i <= N; i++){ dp[i][0] = 1;//前N个硬币都能达到价值0,所以都置为1 }//这里的i要从0开始,因为dp[0][0]要为1,这对后来的dp过程很重要 for (int i = 1; i <= N; i++){ for (int j = price[i]; j <= M; j++){ if (dp[i - 1][j - price[i]] == 1){ dp[i][j] = 1; pre[i][j] = 1; } else{ dp[i][j] = dp[i - 1][j]; } } }//dp过程 if (!dp [M]) printf("No Solution\n"); else{ int count = 0; while (N> 0){ if (pre [M] == 1){ ans[count] = price ;//因为是从N即最后开始取的,所以需要输入降序排列,这样才会使取得结果最小。 M = M - price ; N--; count++; } else N--; } for (int i = 0; i < count - 1; i++){ printf("%d ", ans[i]); } printf("%d\n", ans[count - 1]); } return 0; }
input info
4 9
1 3 4 5
dp[i][j] info
0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 1
0 0 1 1 1 0 1 1 1
1 0 1 1 1 1 1 1 1
pre[i][j] info
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 1 1 0
1 0 0 1 1 1 0 1 1
result info
1 3 5
截图:
DFS的,有一个超时了:
——Apie陈小旭
相关文章推荐
- SQL用法
- android:padding和android:margin的区别
- phpMyAdmin4.4.7安装及配置
- phpMyAdmin4.4.7安装及配置
- java 中的 final关键字
- 理解红黑树算法
- 详解C中volatile关键字
- VVDocumenter-Xcode 注释插件安装
- onCreateOptionsMenu的二次调用
- 函数调用常规二维数组和动态二维数组的差异
- iOS包管理工具Cocoapods的安装与使用
- 栈的压入、弹出序列
- 一个游戏数值策划的自白:我所理解的数值工作
- tomcat记录控制台输出信息至文件
- (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇
- 通过NSString初始化OC类
- php数组与字符串的转换
- Bestcoder#54 A problem of sorting
- 性能安全之新安装的MySQL必须调整的10项配置
- 静态链表实现