PAT A 1068. Find More Coins (30)

题目

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(i , j )为到第i个货币时,当总价值小于等于j时可以取得的最大价值,则:

f(i , j ) = max ( f ( i – 1 , j ) ,f ( i –1 , j – coin [i] ) + coin [i] );       coin[i]为第i个货币的价值。

由于m不大于100,而货币价值不小于1,所以只需要取价值最小的100个货币计算就可以了。

需要最小序列,故把最小的100个货币倒序后进行0-1背包问题求解即可。

这里的序列最小指的是长度。

 

代码:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int n,m;
	int num[101]={0};	//价值0~100的货币各自的数量
	int coin[100];		//价值小于等于100的货币中,价值最小的100个货币(可能不足100个)
	cin>>n>>m;	//输入数据
	
	int coin_num;		//价值小于等于100的货币数目不足100时的数量,可以取足100时为100
	int i,j,k,temp;
	for(i=0;i<n;i++)	//输入数据
	{
		scanf("%d",&temp);
		if(temp<=100)
			num[temp]++;
	}
	for(i=1,j=0;i<=100;i++)	//取小于等于100的中最小的100个
	{
		for(k=0;k<num[i];k++)
		{
			coin[j++]=i;
			if(j>=100)
				break;
		}
	}
	coin_num=j;
	if(coin_num==0)	//有效货币数为0
	{
		cout<<"No Solution";
		return 0;
	}
	reverse(coin,coin+coin_num);	//倒序,大的在前
	
	int **chart=new int* [coin_num];	//chart[i][j],到第i个货币,价值不超过j时可以取得的最大价值
	for(i=0;i<coin_num;i++)	//初始化
	{
		chart[i]=new int [m+1];
		chart[i][0]=0;
	}
	for(j=0;j<m+1;j++)
	{
		if(coin[0]<=j)
			chart[0][j]=coin[0];
		else
			chart[0][j]=0;
	}
	for(i=1;i<coin_num;i++)	//背包问题求解
	{
		for(j=1;j<m+1;j++)
		{
			if(j-coin[i]>=0)
				temp=chart[i-1][j-coin[i]]+coin[i];
			else
				temp=0;
			if(chart[i-1][j]>temp)
				chart[i][j]=chart[i-1][j];
			else
				chart[i][j]=temp;
		}
	}

	if(chart[coin_num-1][m]<m)	//判断是否可以取出序列
	{
		cout<<"No Solution";
		for(i=0;i<coin_num;i++)	//退出
			delete [] chart[i];
		delete [] chart;
		return 0;
	}
	vector<int> seq;
	i=coin_num-1;
	j=m;
	while(j>0&&i>0)	//回溯求序列
	{
		if(chart[i-1][j-coin[i]]==j-coin[i])	//相应货币在最小序列中
		{
			seq.push_back(coin[i]);
			j-=coin[i--];
		}
		else
			i--;
	}
	if(i==0&&j!=0)	//考虑0号货币,如果货币从1开始编号,并对0进行初始化可以简化
		seq.push_back(coin[i]);

	k=0;	//输出序列
	printf("%d",seq[k++]);
	while(k<seq.size())
		printf(" %d",seq[k++]);

	for(i=0;i<coin_num;i++)	//退出
		delete [] chart[i];
	delete [] chart;

	return 0;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值