1068. Find More Coins (30)

题目链接: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:
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[N];
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[N][M])
printf("No Solution\n");
else{
int count = 0;
while (N> 0){
if (pre[N][M] == 1){
ans[count] = price[N];//因为是从N即最后开始取的,所以需要输入降序排列,这样才会使取得结果最小。
M = M - price[N];
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陈小旭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值