【POJ3260】The Fewest Coins 多重背包+完全背包

A来B处买东西,价值M元,有N种钱,每种钱A有一定数量,而B有无限数量。

求最少用多少张钞票可以满足交易,比如样例,A出50+25,B找5,即可满足,需要3张。

A用多重背包转移状态,B用完全背包。

本文的多重背包优化用的是O(n)算法,二进制转换的O(nlogn)实在懒得写了。

那种可以看http://blog.csdn.net/vmurder/article/details/39472419

【POJ1014】Dividing 多重背包,二进制物品拆分转01背包

贴代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 150
#define M 20000
#define inf 0x3f3f3f3f
using namespace std;

int n,m,ans=inf;
int w[N],num[N];
int f1[M],cnt[M],f2[M];
int main()
{
//	freopen("test.in","r",stdin);
	int i,j,k;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)scanf("%d",&w[i]);
	for(i=1;i<=n;i++)scanf("%d",&num[i]);
	memset(f1,0x3f,sizeof(f1));
	memset(f2,0x3f,sizeof(f2));
	f1[0]=f2[0]=0;
	for(i=1;i<=n;i++)
	{
		memset(cnt,0,sizeof(cnt));
		for(j=w[i];j<=15000;j++)
		{
			if(f1[j]>f1[j-w[i]]+1&&cnt[j-w[i]]<num[i])
			{
				f1[j]=f1[j-w[i]]+1;
				cnt[j]=cnt[j-w[i]]+1;
			}
			f2[j]=min(f2[j],f2[j-w[i]]+1);
		}
	}
	for(i=m;i<=15000;i++)
	{
		ans=min(ans,f1[i]+f2[i-m]);
	}
	if(ans>=15000)printf("-1\n");
	else printf("%d\n",ans);
	return 0;
}

复制去Google翻译翻译结果

时间: 2024-10-08 10:23:16

【POJ3260】The Fewest Coins 多重背包+完全背包的相关文章

POJ3260——The Fewest Coins(多重背包+完全背包)

The Fewest Coins DescriptionFarmer 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

POJ 3260 The Fewest Coins(多重背包+全然背包)

http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币,相应面值为val[1],val[2]-val[n]. 然后他身上每种货币有num[i]个. John必须付给售货员>=m的金钱, 然后售货员会用最少的货币数量找钱给John. 问你John的交易过程中, 他给售货员的货币数目+售货员找钱给他的货币数目 的和最小值是多少? 分析: 本题与POJ 1252类型: http://blog.csdn.net/u013480600

poj 3260 The Fewest Coins 多重背包+完全背包

题目链接:http://poj.org/problem?id=3260 给店家的钱是多重背包 dp[] 店家的找钱是完全背包 dp2[] 然后最后 其中i表示多给了多少钱 也就是需要找回多少钱 int ans = INF; ans = min(ans, dp[m+i] + dp2[i]); 是一个比较简单的思路 神坑题 看到每种货币的面值不大于120 我就觉得找钱一定不会超过119块钱结果wa到死 后来才发现不是的啊 之所以我有这种思维定式是因为现实生活中有1块钱这种货币单位 考虑一种极限的情况

POJ-3260 The Fewest Coins

题目链接:POJ-3260 题意是一个人买东西,有n种纸币,面额为v[i],数量为c[i].同时售货员也有这些纸币,数量为无限.要买价值为t的东西,希望给"钱用的纸币数和着钱用的纸币数的和"最少. 思路很显然是完全背包和多重背包各处理售货员和这位老哥.这个题给出所有v[i]<=120,这一点很有迷惑性. 一开始我的做法是把上界设成t+120,但是发现这样的做法是不多的.网上给出的做法是设上界为maxv*maxv+t.证明过程如下: 要凑足(大于等于)价格T的商品且硬币数最少,最多

poj3260 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 co

POJ3260The Fewest Coins[背包]

The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 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 sm

POJ 1742 Coins (多重背包)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some

POJ 1742 Coins 多重背包单调队列优化

http://poj.org/problem?id=1742 题意: 很多硬币,有价值和数量,给出一个上限,问上限内有多少种钱数可以由这些硬币组成. 分析: 好像是楼教主男人八题之一.然后学多重背包单调队列优化时看了别人的程序..所以后来写了就1A了=.= 前一篇小小总结了一下多重背包单调队列优化(http://www.cnblogs.com/james47/p/3894772.html),这里就不写了. 1 #include<cstdio> 2 #include<cstring>

混合背包(多重背包+完全背包)—— POJ 3260

对应POJ题目:点击打开链接 The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5450   Accepted: 1653 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