PAT (Advanced Level) 1068. Find More Coins (30) DFS+剪枝 或 动态规划

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+剪枝,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;
}

上网看了一下,说是背包问题,可以用动态规划。研究研究,等会再来更新。
来了,我是分割线*************************************************************
动态规划:
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[N][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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#include <iostream>#include <cstdlib>using namespace std;struct Coin { int weight; // 硬币的重量 bool is_fake; // 是否为假币};Coin coins[100];// 将硬币分成两等份并比较的功能,返回较轻的那一半硬币的重量总和int balance(int start, int end) { int sum = 0; for (int i = start; i <= end; i++) { sum += coins[i].weight; } return sum;}int find_fake_coin(int start, int end) { int len = end - start + 1; if (len == 2) { // 只剩下两个硬币 if (coins[start].weight < coins[end].weight) { return start; } else { return end; } } else if (len == 3) { // 只剩下三个硬币 int index = rand() % 3 + start; if (index == start) { // 取出第一枚硬币作为样本 if (coins[start + 1].weight == coins[start + 2].weight) { return start; } else if (coins[start + 1].weight < coins[start + 2].weight) { return start + 1; } else { return start + 2; } } else if (index == start + 1) { // 取出第二枚硬币作为样本 if (coins[start].weight == coins[start + 2].weight) { return start + 1; } else if (coins[start].weight < coins[start + 2].weight) { return start; } else { return start + 2; } } else { // 取出第三枚硬币作为样本 if (coins[start].weight == coins[start + 1].weight) { return start + 2; } else if (coins[start].weight < coins[start + 1].weight) { return start; } else { return start + 1; } } } else { // 将硬币分成两等份并比较 int mid = (start + end) / 2; int left_sum = balance(start, mid); int right_sum = balance(mid + 1, end); if (left_sum < right_sum) { return find_fake_coin(start, mid); } else if (left_sum > right_sum) { return find_fake_coin(mid + 1, end); } else { return -1; // 不可能出现的情况 } }}int main() { int n; cout << "请输入硬币的数量n:"; cin >> n; srand(time(NULL)); int fake_index = rand() % n; // 随机生成假币的位置 for (int i = 0; i < n; i++) { coins[i].weight = 10; // 正常硬币的重量为10g if (i == fake_index) { coins[i].weight = 8; // 假币的重量为8g coins[i].is_fake = true; } else { coins[i].is_fake = false; } } int fake = find_fake_coin(0, n - 1); cout << "假币的位置是:" << fake << endl; return 0;}求此算法的运行结果和算法时间复杂度
06-08
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值