POJ-3260-The Fewest Coins
Description
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 V1, V2, …, VN coins (V1, …VN)
Line 3: N space-separated integers, respectively C1, C2, …, 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.
题意
- 输入一个N一个V,代表有N种硬币和货物的价值V
- 接下来有一行N中硬币各自的面值,以及一行N中硬币各自的数量
- 现在顾客有N种硬币,每种硬币的数量即输入给定
- 老板也是有N种硬币,但每种硬币的数量无限
- 要求顾客购买价值为V的货物,付钱给老板后老板找零
- 输出顾客经手的最小硬币数量(付钱+找零)
题解
- 这是混合背包的模板题,对模板进行微改即可
- 处理付钱过程(顾客)的每种额度的最小硬币数量属于多重背包(硬币数量给定)
- 处理找零过程(老板)的每种额度的最小硬币数量属于完全背包(硬币数量无限)
- 最后遍历所有可能的付钱额度(从V到上界),输出的ans通过**ans=min(ans,dp1[i]+dp2[i-v]);**即可求得
- 因为 顾客付钱 i 元,老板找零即 i-v 元( i 必须大于 v )
- 求 付钱额度的上界 可用鸽笼原理所得的 硬币的最大面值平方+货物价值V(详见代码行注释)
- 也可用 货物价值V+10000左右,开得较大但不至于过大即可
- 上界太小:可能存在一种硬币面值比所定上界大;上界太大:循环后时间复杂度过大,导致TLE
涉及知识点
- 背包 算法(此题属于混合背包:多重背包+完全背包)
- 对于背包的算法-详见链接博客介绍背包
AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6+2e5+1e4+10;
const int inf=0x3f3f3f3f;
int T,n,v,value[maxn],value1[maxn],cost[maxn],num[maxn],dp1[maxn],dp2[maxn],Max;
void knapsack_multiple()//多重背包,顾客
{
int n1=0;
memset(value1,0,sizeof(value1));
memset(dp1,inf,sizeof(dp1));
memset(num,inf,sizeof(num));
dp1[0]=0;
for(int i=1;i<=n;++i)
{
for(int j=1;cost[i];j*=2)
{
j=min(j,cost[i]);
cost[i]-=j;
n1++;
value1[n1]=value[i]*j;
num[n1]=j;
}
}
for(int i=1;i<=n1;++i)
{
for(int j=Max*Max+v;j>=value1[i];--j)
{
dp1[j]=min(dp1[j],dp1[j-value1[i]]+num[i]);
}
}
}
void knapsack_full()//完全背包,老板
{
memset(dp2,inf,sizeof(dp2));
dp2[0]=0;
for(int i=1;i<=n;++i)
{
for(int j=value[i];j<=Max*Max;++j)
{
dp2[j]=min(dp2[j],dp2[j-value[i]]+1);
}
}
}
int main()
{
while(~scanf("%d %d",&n,&v))
{
Max=0;
memset(value,0,sizeof(value));
memset(cost,0,sizeof(cost));
for(int i=1;i<=n;++i)
{
scanf("%d",&value[i]);//面值
Max=max(Max,value[i]);
}
for(int i=1;i<=n;++i) scanf("%d",&cost[i]);//顾客手中的数量
knapsack_multiple();
knapsack_full();
int ans=inf;
for(int i=v;i<=Max*Max+v;++i)
{
ans=min(ans,dp1[i]+dp2[i-v]);
}
if(ans==inf) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
/*
付款数大于了Max*Max+v,即付硬币的数目大于了Max
根据 #鸽笼原理# ,在硬币序列中至少有两个子序列的和对Max取模的值相等
也就是说,这部分硬币能够用Max进行替换使总个数更少
*/