您的位置:首页 > 其它

hdu 2844 coins 多重背包

2016-01-24 16:18 453 查看

Coins

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10759    Accepted Submission(s): 4280
[/b]

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case output the answer on a single line.

 

[align=left]Sample Input[/align]

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

 

[align=left]Sample Output[/align]

8
4

 

[align=left]Source[/align]
2009 Multi-University Training Contest 3 - Host by WHU 

思路:其实就是多重背包的应用,只是这里价值和重量是相等的,因此最后计数是要计价值和重量相等的个数
不懂背包可以看一下这个背包9讲  http://blog.csdn.net/pi9nc/article/details/8142876
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
using namespace std;
int dp[100005];
vector <int> vc;
set<int> st;
int a[105], c[105];
int n, m;
void CompletePack (int value)// 完全背包
{
for(int i = value; i <= m; i ++)
{
dp[i] = max(dp[i - value] + value, dp[i]);
}
}

void ZeroOnePack(int value) //01背包
{
for(int i = m ; i >= value; i --)
{
dp[i] = max(dp[i - value] + value, dp[i]);
}
}

void MultiplePack(int value, int number) //多重背包
{
if(value * number >= m) //相当于无穷个,用完全背包
{
CompletePack(value);
return ;
}
int k = 1;
while(k < number)
{
ZeroOnePack(k * value);
number -= k;
k <<= 1;
}
ZeroOnePack(number * value);
}

int main()
{
int i, j;
while(~scanf("%d%d", &n, &m) && (n&&m))
{
for(i = 1; i <= n; i ++)
{
scanf("%d", &a[i]);
}
for(i = 1; i <= n; i ++)
{
scanf("%d", &c[i]);
}
memset(dp, 0, sizeof(dp));
for(i = 1; i <= n ;i ++)
{
MultiplePack(a[i], c[i]);
}
int ans = 0;
for(i = 1; i <= m; i ++)
{
if(dp[i] == i)   //因为这个背包的容量就是价值
ans ++;
//	printf("%d ", dp[i]);
}
printf("%d\n", ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: