天天看點

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).

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,同時店主也隻有這幾種面額的硬币,但數量無限,問約翰總共要經手的硬币數量(約翰買東西給店主的硬币數量+店主找錢給約翰的硬币數量=約翰經手的硬币數量)(約翰是多重背包,店主是完全背包)

主要難點在于那個人應該付多少錢?付錢的上界是什麼?

付錢的上界為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。

找到上界之後就是多重背包和完全背包了

#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;
}