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).
Input
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V 1, V 2, …, VN coins ( V 1, … VN)
Line 3: N space-separated integers, respectively C 1, C 2, …, CN
Output
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.
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
Hint
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.
题意:农夫约翰要购买价格为T的物品,他有N种硬币,每种硬币的面额为Vi,数量为Ci,同时店主也只有这几种面额的硬币,但数量无限,问约翰总共要经手的硬币数量(约翰买东西给店主的硬币数量+店主找钱给约翰的硬币数量=约翰经手的硬币数量)(约翰是多重背包,店主是完全背包)
<mark>主要难点在于那个人应该付多少钱?付钱的上界是什么?</mark>
付钱的上界为maxV*maxV+T(maxV是最大面额的纸币)
网上大神的证明:
假设顾客给的钱超过T+maxVmaxV,则店家找的钱肯定超过maxVmaxV,找的钱币数n超过maxV。
设店家找的钱币为一序列,则其前n项和为sum(n),则根据鸽巢原理,至少存在两个sum(i)和sum(j)对maxV取余值相同(因为sum%maxV<maxV且n>maxV)。
则sum(j)-sum(i)为maxV的倍数,同理,在买家付的钱中也存在一部分这样的钱,则买家和卖家这部分可约去得到更优解。
故上限为T+maxV*maxV。
<mark>找到上界之后就是多重背包和完全背包了</mark>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int money[105],num[105];//num是money的数目
int newmoney[10005],newnum[10005];//这个newnum是指这个newmoney实际上是几个money
int dp1[100005];//约翰对不同金额所付的最少硬币数量
int dp2[100005];//店长对不同金额所找的最少硬币数量
const int Inf=999999999;
int main(){
int N,T;
scanf("%d%d",&N,&T);
int maxmoney=0;
for(int i=1;i<=N;i++){
scanf("%d",&money[i]);
maxmoney=max(maxmoney,money[i]);
}
for(int i=1;i<=N;i++)
scanf("%d",&num[i]);
int cnt=1;
for(int i=1;i<=N;i++){ //多重背包转01背包
for(int j=1;j<=num[i];j<<=1){
newmoney[cnt]=money[i]*j;
newnum[cnt]=j;
cnt++;
num[i]-=j;
}
if(num[i])
{
newmoney[cnt]=money[i]*num[i];
newnum[cnt]=num[i];
cnt++;
}
}
maxmoney=maxmoney*maxmoney+T;
for(int i=1;i<=maxmoney;i++)
dp1[i]=dp2[i]=Inf;
dp1[0]=dp2[0]=0;
for(int i=1;i<cnt;i++){ //01背包 付钱
for(int j=maxmoney;j>=newmoney[i];j--){
dp1[j]=min(dp1[j],dp1[j-newmoney[i]]+newnum[i]);
}
}
for(int i=1;i<=N;i++){ //完全背包 找钱
for(int j=money[i];j<=maxmoney;j++){
dp2[j]=min(dp2[j],dp2[j-money[i]]+1);
}
}
int ans=Inf;
for(int i=T;i<=maxmoney;i++){
ans=min(ans,dp1[i]+dp2[i-T]);
}
if(ans==Inf)
printf("-1\n");
else
printf("%d\n",ans);
return 0;
}