北大ACM1742——Coins~~多重背包

题目的意思是,输入n个硬币的面值和数量,求不超过m的组合方法的数目。

今天学习了多重背包,但是还是不是很懂,继续努力。

下面的是AC的代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

class data
{
public:
	int value, count;
};
data Da[101];
int dp[100005];
int sum[100005];
int m, n;
int main()
{
	int i, j;
	while(scanf("%d%d", &n, &m) != EOF && m + n != 0)
	{
		for(i = 0; i < n; i++)
		{
			scanf("%d", &Da[i].value);
		}
		for(i = 0; i < n; i++)
		{
			scanf("%d", &Da[i].count);
		}
		memset(dp, 0, sizeof(dp));
		dp[0] = 1;
		int ans = 0;
		for(i = 0; i < n ; i++)
		{
			memset(sum, 0, sizeof(sum));
			for(j = Da[i].value; j <= m; j++)
			{
				if(!dp[j] && dp[j - Da[i].value] && sum[j - Da[i].value] < Da[i].count)
				{
					dp[j] = 1;
					sum[j] = sum[j - Da[i].value] + 1;
					ans++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-07-29 18:45:58

北大ACM1742——Coins~~多重背包的相关文章

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>

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

POJ1742 Coins[多重背包可行性]

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 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

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

点击打开链接 Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167    Accepted Submission(s): 3327 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dolla

hdu2844 coins 多重背包

1 #include<stdio.h> 2 #include<string.h> 3 int a[102],c[102],dp[100005]; 4 int max(int a,int b) 5 { 6 return a>b?a:b; 7 } 8 void CompletePack(int v,int w,int m) //完全背包 9 { 10 for(int j=v;j<=m;j++) 11 dp[j]=max(dp[j],dp[j-v]+w); 12 } 13 v

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块钱这种货币单位 考虑一种极限的情况

HDu -2844 Coins多重背包

这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就是dp[i] = i, 其中dp[i]表示当前背包容量为i 的时候背包能装的价值. 题目思路: 模板 二进制优化 话说那个二进制真的很奇妙,只需要2的1次方 到 2的k-1次方, 到最后在加上一项当前项的个数 - 2 的k次方 + 1,也就是这些系数分别为1; 2; 22 .....2k-1;Mi