USACO SECTION 2.3 Money Systems

Money Systems

The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.

The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.

Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).

PROGRAM NAME: money

INPUT FORMAT

The number of coins in the system is V (1 <= V <= 25).

The amount money to construct is N (1 <= N <= 10,000).

Line 1:Two integers, V and N
Lines 2..:V integers that represent the available coins (no particular number of integers per line)

SAMPLE INPUT (file money.in)

3 10
1 2 5

OUTPUT FORMAT

A single line containing the total number of ways to construct N money units using V coins.

SAMPLE OUTPUT (file money.out)

10

/*
ID: conicoc1
LANG: C
TASK: money
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int CoinsNumber,AmountMoney;
int Money[26];
unsigned long long int Construct[26][10001];     

int main()
{
	FILE *fin,*fout;
	int i,j,k;
	fin=fopen("money.in","r");
	fout=fopen("money.out","w");
	memset(Construct,0,sizeof(Construct));
	fscanf(fin,"%d %d",&CoinsNumber,&AmountMoney);
	
	for(i=1;i<=CoinsNumber;i++)
		fscanf(fin,"%d",&Money[i]);
	for(i=0;i<=CoinsNumber;i++)
		Construct[i][0]=1;
	for(i=1;i<=CoinsNumber;i++){
		for(j=1;j<=AmountMoney;j++){
			for(k=0;k*Money[i]<=j;k++)
				Construct[i][j]+=Construct[i-1][j-k*Money[i]];
		}
	}
	fprintf(fout,"%lld\n",Construct[CoinsNumber][AmountMoney]);
	
	return 0;	
}


传说中的完全背包0 0

看来是时候去看看那个奇葩的背包九讲了= =

 

自己写了之后发现还是没有原话清楚,干脆转一下NOCOW上面完全背包问题的讲解

 

基本思路

这个问题非常类似于01背包问题,所不同的是每种物品有无限件。也就是从每种物品的角度考虑,与它相关的策略已并非取或不取两种,而是有取0件、取1件、取2件……等很多种。如果仍然按照解01背包时的思路,令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值。仍然可以按照每种物品不同的策略写出状态转移方程,像这样:

   f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v}

这跟01背包问题一样有O(VN)个状态需要求解,但求解每个状态的时间已经不是常数了,求解状态f[i][v]的时间是O(v /c[i]),总的复杂度可以认为是O(V*Σ(V/c[i])),是比较大的。

01背包问题的基本思路加以改进,得到了这样一个清晰的方法。这说明01背包问题的方程的确是很重要,可以推及其它类型的背包问题。但我们还是试图改进这个复杂度。


 一个简单有效的优化

完全背包问题有一个很简单有效的优化,是这样的:若两件物品i、j满足c[i]<=c[j]且w[i]> =w[j],则将物品j去掉,不用考虑。这个优化的正确性显然:任何情况下都可将价值小费用高得j换成物美价廉的i,得到至少不会更差的方案。对于随机生成的数据,这个方法往往会大大减少物品的件数,从而加快速度。然而这个并不能改善最坏情况的复杂度,因为有可能特别设计的数据可以一件物品也去不掉。

这个优化可以简单的O(N^2)地实现,一般都可以承受。另外,针对背包问题而言,比较不错的一种方法是:首先将费用大于V的物品去掉,然后使用类似计数排序的做法,计算出费用相同的物品中价值最高的是哪个,可以O(V+N)地完成这个优化。这个不太重要的过程就不给出伪代码了,希望你能独立思考写出伪代码或程序。


转化为01背包问题求解

既然01背包问题是最基本的背包问题,那么我们可以考虑把完全背包问题转化为01背包问题来解。最简单的想法是,考虑到第i种物品最多选V/c[i]件,于是可以把第i种物品转化为V/c[i]件费用及价值均不变的物品,然后求解这个01背包问题。这样完全没有改进基本思路的时间复杂度,但这毕竟给了我们将完全背包问题转化为01背包问题的思路:将一种物品拆成多件物品。

更高效的转化方法是:把第i种物品拆成费用为c[i]*2^k、价值为w[i]*2^k的若干件物品,其中k满足c[i]*2^k<=V。这是二进制的思想,因为不管最优策略选几件第i种物品,总可以表示成若干个2^k件物品的和。这样把每种物品拆成O(log V/c[i])件物品,是一个很大的改进。


O(VN)的算法

但我们有更优的O(VN)的算法。

这个算法使用一维数组,先看伪代码:

for i=1..N
    for v=0..V
        f[v]=max{f[v],f[v-cost]+weight}

你会发现,这个伪代码与01背包的伪代码只有v的循环次序不同而已。为什么这样一改就可行呢?首先想想为什么01背包中要按照v=V..0的逆序来循环。这是因为要保证第i次循环中的状态f [i][v]是由状态f[i-1][v-c[i]]递推而来。换句话说,这正是为了保证每件物品只选一次,保证在考虑“选入第i件物品”这件策略时,依据的是一个绝无已经选入第i件物品的子结果f[i-1][v-c[i]]。而现在完全背包的特点恰是每种物品可选无限件,所以在考虑“加选一件第i种物品” 这种策略时,却正需要一个可能已选入第i种物品的子结果f[i][v-c[i]],所以就可以并且必须采用v=0..V的顺序循环。这就是这个简单的程序为何成立的道理。

值得一提的是,上面的伪代码中两层for循环的次序可以颠倒。这个结论有可能会带来算法时间常数上的优化。

这个算法也可以以另外的思路得出。例如,将基本思路中求解f[i][v-c[i]]的状态转移方程显式地写出来,代入原方程中,会发现该方程可以等价地变形成这种形式:

   f[i][v]=max{f[i-1][v],f[i][v-c[i]]+w[i]}

将这个方程用一维数组实现,便得到了上面的伪代码。

最后抽象出处理一件完全背包类物品的过程伪代码:

procedure CompletePack(cost,weight)
    for v=cost..V
        f[v]=max{f[v],f[v-c[i]]+w[i]}

 

 

 然后我们根据讲解得出了如下优化后的代码,相当简洁

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int CoinsNumber,AmountMoney;
int Money[26];
unsigned long long int Construct[10001];     

int main()
{
	FILE *fin,*fout;
	int i,j,k;
	fin=fopen("money.in","r");
	fout=fopen("money.out","w");
	memset(Construct,0,sizeof(Construct));
	fscanf(fin,"%d %d",&CoinsNumber,&AmountMoney);
	
	for(i=1;i<=CoinsNumber;i++)
		fscanf(fin,"%d",&Money[i]);
	Construct[0]=1;
	for(i=1;i<=CoinsNumber;i++){
		for(j=Money[i];j<=AmountMoney;j++){
			Construct[j]+=Construct[j-Money[i]];
		}
	}
	fprintf(fout,"%lld\n",Construct[AmountMoney]);
	
	return 0;	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值