HDU 2844 二进制优化的多重背包

Coins

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

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

2009 Multi-University Training Contest 3 - Host by WHU

题意:有不同面值的 相应数量不同n种硬币  问1~m元的价格有哪些 可以由硬币组成 f[i]==i

题解: 多重背包    二进制优化

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<map>
 5 #include<queue>
 6 #include<stack>
 7 using namespace std;
 8 int n,m;
 9 struct node
10 {
11     int a,c;
12 } N[105];
13 int f[110005];
14 int ans;
15 int value[100005],size[100005];
16 int count;
17 void slove(int q)
18 {
19     count=1;
20    for(int i=1;i<=q;i++)
21   {
22      int c=N[i].c,v=N[i].a;
23     for (int k=1; k<=c; k<<=1)
24     {
25         value[count] = k*v;
26         size[count++] = k*v;
27         c -= k;
28     }
29     if (c > 0)
30     {
31        value[count] = c*v;
32        size[count++] = c*v;
33     }
34 }
35 }
36 int main()
37 {
38     while(scanf("%d %d",&n,&m)!=EOF)
39     {
40         if(n==0&&m==0)
41            break;
42         for(int i=0; i <= m; i++)
43             f[i] = 0;
44         ans=0;
45         for(int i=1;i<=n;i++)
46         scanf("%d",&N[i].a);
47         for(int i=1;i<=n;i++)
48         scanf("%d",&N[i].c);
49         slove(n);
50         for(int i=1;i<count;i++)
51          for(int gg=m;gg>=size[i];gg--)
52           f[gg]=max(f[gg],f[gg-size[i]]+value[i]);
53         for(int i=1;i<=m;i++)
54           if(f[i]==i)
55           ans++;
56         cout<<ans<<endl;
57     }
58     return 0;
59 }
时间: 2024-08-26 09:28:41

HDU 2844 二进制优化的多重背包的相关文章

HDU 3591 (完全背包+二进制优化的多重背包)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2798    Accepted Submission(s): 972 Problem Description In the countr

HDU 2191 悼念512【多重背包+二进制优化】

大意分析: 多重背包,转化为01背包即可 可以用二进制进行优化 代码:(代码没有优化,下题是优化才可过的) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 105; 7 8 int n, m, tot; 9 int p[2005], h[2005]; 10 int dp[maxn]; 11 int solv

HDU #2191 买米问题 多重背包及其优化

Description 问题描述以及测试样例在这:HDU#2191 思路 这道题其实就是多重背包问题,即有 N 种物品和一个容量为 V 的背包,第 i 种物品最多有 n[i] 件可用,每件费用是 c[i] ,价值是 w[i] ,求哪些物品装入背包可以使得这些物品的费用总和不超过背包容量,且价值总和最大.在这道题中,背包容量是经费,费用是每种米的价格,价值是每种米的重量,求用给定的经费买米,使得买到米的总重量最大,注意经费是可以剩余的. 其实多重背包问题和完全背包问题非常相似,只不过前者的物品是有

HDU 2159 FATE (二维多重背包)

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9352    Accepted Submission(s): 4413 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问

HDU 2082 找单词 (多重背包)

题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的单词呢?单词的价值就是组成一个单词的所有字母的价值之和,比如,单词ACM的价值是1+3+14=18,单词HDU的价值是8+4+21=33.(组成的单词与排列顺序无关,比如ACM与CMA认为是同一个单词). 题解:把26个字母看做26种背包,有个数有价值,求价值不超过50的所有可能个数,就是标准的多重背包.

HDU 3732 Ahui Writes Word(多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=3732 题意: 初始有N个物品, 每个物品有cost[i]花费和val[i]价值, 你有m元钱, 现在问你最多能买多少总价值的物品? 其中N<=10W, m<=1W. 且cost[i]和val[i]都在[0,10]范围. 分析: 本题初看直接用01背包来做是直观的想法. 但是考虑到01背包的复杂度为O(N*m), 这么大的复杂度肯定不行. 然后我们发现其实每种物品只与它的cost[i]和val[i]有关, 如

ACM学习历程—HDU 1059 Dividing(dp &amp;&amp; 多重背包)

Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just spl

hdu 1059 Dividing DP,多重背包 测试数据很水

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18691    Accepted Submission(s): 5214 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers