hdu-2844 Cions-多重背包

原创 2018年04月17日 21:29:09

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18336    Accepted Submission(s): 7208


Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
 

Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 

Output
For each test case output the answer on a single line.
 

Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
 

Sample Output
8
4
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2845 2870 2577 2830 1069 

#include<stdio.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int f[1000010];//满足价值不超过价值 i的最大价值 
int a[105],c[105];
int n,m;

//m-背包容量,v-物品体积,w-物品价值,num-物品数量
//01背包 
void OneZeroPack(int m,int v,int w){
	for(int i=m;i>=v;i--){
		f[i]=max(f[i],f[i-v]+w);
	} 
} 
//完全背包 
void CompletePack(int m,int v,int w){
	for(int i=v;i<=m;i++){
		f[i]=max(f[i],f[i-v]+w);
	}
}
//多重背包  
void MutiplePack(int m,int v,int w,int num){
	if(v*num>=m){
		CompletePack(m,v,w);
		return ;
	} 
	int k=1;
	for(k=1;k<=num;k<<=1)
	{
		OneZeroPack(m,k*v,k*w);
		num-=k; 
	}
	if(num)
	  OneZeroPack(m,num*v,num*w);
}
int main()
{
	while(~scanf("%d%d",&n,&m)&&(n||m))
	{
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		for(int i=0;i<n;i++)
			scanf("%d",&c[i]);
		for(int i=0;i<=m;i++)
			f[i]=-inf;
		f[0]=0;
		for(int i=0;i<n;i++)
		{
			MutiplePack(m,a[i],a[i],c[i]);
		}
		int sum=0;
		for(int i=1;i<=m;i++)
			if(f[i]>0)
			sum++;
		printf("%d\n",sum);
	}
	return 0; 
}

hdu2844 Coins(多重背包+二进制优化)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目大意: 有n种价值不同的硬币,他们的数量有限但不相同。现在要求出硬币组合,可组合出多少种...
  • aaaaacmer
  • aaaaacmer
  • 2015-09-18 14:10:08
  • 837

HDU-2844 多重背包

我想知道200ms以内的算法 :) 也许是单调队列? 还没学... /* * HDU-2844 coins * mike-w * 2011-10-15 * ---------------...
  • creativewang
  • creativewang
  • 2011-10-15 21:26:17
  • 320

HDU-2844 - Coins - 多重背包

链接:  http://acm.hdu.edu.cn/showproblem.php?pid=2844题目:Problem DescriptionWhuacmers use coins.They ha...
  • xs18952904
  • xs18952904
  • 2017-09-19 17:14:40
  • 168

HDU-2844 Coins (多重背包)

题目: Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix open...
  • robin_8
  • robin_8
  • 2017-01-04 12:39:10
  • 299

HDU-2844(多重背包中二进制优化)

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened p...
  • qq_36346262
  • qq_36346262
  • 2017-03-29 22:56:57
  • 62

HDU-2844 Coins (多重背包+完全背包)

一道完全背包+多重背包的题目,需要注意一下优化复杂度。给你100种硬币和每种硬币的个数,不超过1000个,硬币价格不超过1e5,问你用这些硬币可以组成多少种不同的价格。价格区间1~1e5。...
  • hmc0411
  • hmc0411
  • 2017-11-14 20:06:53
  • 712

多重背包单调队列优化问题.ppt

  • 2018年04月07日 12:24
  • 130KB
  • 下载

HDU 2844 Coins 多重背包判断可行性

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2844题意:有很多种钱币,每种都有特定的价值和数量,求用这些钱币可以凑出多少个在1~m之间的数字思路定义dp...
  • discreeter
  • discreeter
  • 2016-11-29 18:45:47
  • 242

背包模板(01,完全,多重背包的二进制优化和单调队列优化

背包问题 1,01背包 背包问题的基础,总体积为V的背包,有n件体积v【i】,价值w【i】的物品,求能装物品的最大总价值 void zero(int v,int w) { for(int j=V;j...
  • jerans
  • jerans
  • 2017-02-08 00:15:18
  • 838

多重背包O(N*V)算法详解(使用单调队列)

多重背包问题:有N种物品和容量为V的背包,若第i种物品,容量为v[i],价值为w[i],共有n[i]件。怎样装才能使背包内的物品总价值最大?网上关于“多重背包”的资料倒是不少,但是关于怎么实现O(N*...
  • flyinghearts
  • flyinghearts
  • 2010-09-20 23:41:00
  • 13313
收藏助手
不良信息举报
您举报文章:hdu-2844 Cions-多重背包
举报原因:
原因补充:

(最多只允许输入30个字)