首页 > 题解 > luogu2851 最少的硬币The Fewest Coins

luogu2851 最少的硬币The Fewest Coins

题目描述

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, …, VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 coins of value V2, …., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

农夫John想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。 John想要买T(1<=T<=10000)样东西。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2..Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。我们假设店主有无限多的硬币, 并总按最优方案找零。

输入输出格式

输入格式:

Line 1: Two space-separated integers: N and T.

Line 2: N space-separated integers, respectively V1, V2, …, VN coins (V1, …VN)

Line 3: N space-separated integers, respectively C1, C2, …, CN

输出格式:

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

输入输出样例

输入样例#1:

3 70
5 25 50
5 2 1

输出样例#1:

3

说明

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

题解

这题还是有点意思的。

我们发现John给老板钱的时候,是一个多重背包,老板找钱的时候,是一个完全背包。

现在就是确定背包容量的上下界。假如直接无脑加起来,那么肯定T了。

然后就证明一下

上界为:$T+maxValue^2$,其中$maxValue$为最大硬币面值。

证明:反证法。

假设存在一种支付方案,John给的钱超过$T+maxValue^2$, 则售货员找零超过$maxValue^2$,找的硬币数目超过$maxValue$个,将其看作一数列,求前n项和$sum(n)$,根据鸽巢原理,至少有两个对$maxValue$求模的值相等,假设为$sum(i)$和$sum(j),i< j$,则$i+1…j$的硬币面值和为$maxValue$的倍数,同理,John给的钱中也有一定数量的硬币面值和为$maxValue$的倍数,则这两堆硬币可用数量更少的$maxValue$面值硬币代替,产生更优方案。

然后就是背包了。注意要二进制优化一下


如果你觉的这篇文章不错,分享给朋友吧!

打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮

×