HDU2844_Coins【多重背包】【二进制优化】
来源:互联网 发布:剑网三南风脸型数据 编辑:程序博客网 时间:2024/06/03 21:55
Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7497 Accepted Submission(s): 3055
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
题目大意:给你几种硬币的价值和数量,再给你一个最大钱数M,问你这些硬币能
组成价值1到M的值有多少种
思路:简单的多重背包,如果总容量比这个物品的容量要小,那么这个物品可以直
接取完,相当于完全背包。否则的话就转成01背包来求解。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int v[110],c[110];int dp[100010],V;//v数组存价值,c数组存数量,V是总容量void ZeroOne(int cost,int weight)//01背包{ for(int i = V; i >= cost; i--) dp[i] = max(dp[i],dp[i-cost]+weight);}void Complete(int cost,int weight)//完全背包{ for(int i = cost; i <= V; i++) dp[i] = max(dp[i],dp[i-cost]+weight);}void Multiple(int cost,int weight,int cnt)//多重背包{ //如果总容量比这个物品的容量要小,那么这个物品可以直接取完,相当于完全背包 if(V <= cnt*cost) { Complete(cost,weight); return; } else//否则就将多重背包转化为01背包 { int k = 1; while(k <= cnt) { ZeroOne(k*cost,k*weight); cnt -= k; k <<= 1; } ZeroOne(cnt*cost,cnt*weight); }}int main(){ int N; while(~scanf("%d%d",&N,&V)&&(N!=0||V!=0)) { for(int i = 0; i < N; i++) scanf("%d",&v[i]); for(int i = 0; i < N; i++) scanf("%d",&c[i]); for(int i = 0; i <= V; i++)//初始化:是否恰好装满背包 dp[i] = -0xffffff0; dp[0] = 0; for(int i = 0; i < N; i++) Multiple(v[i],v[i],c[i]); int ans = 0; for(int i = 1; i <= V; i++) if(dp[i] >= 0) ans++; printf("%d\n",ans); } return 0;}
0 0
- HDU2844_Coins【多重背包】【二进制优化】
- 多重背包二进制优化
- 多重背包二进制优化
- 多重背包二进制优化
- 多重背包--二进制优化
- hdu1059Dividing 多重背包二进制优化
- HDU1059Dividing 二进制优化多重背包
- Dividing(多重背包+二进制优化)
- HDOJ2844Coins【多重背包+二进制优化】
- POJ1014Dividing【多重背包+二进制优化】
- hdu3732 多重背包+二进制优化
- 多重背包(二进制优化)
- 多重背包的二进制优化
- 多重背包(二进制优化)
- hdu2844 多重背包+二进制优化
- 多重背包的二进制优化
- 多重背包(二进制优化)
- 多重背包的二进制优化
- OpenCV函数笔记
- ejoy
- Wordpress深度自定义tag页面Permalink的方法
- 【Cocos2d-X3.x游戏开发】之一:初探Cocos,Eclipse Android环境搭建
- 第9周项目5程序填充题5-2
- HDU2844_Coins【多重背包】【二进制优化】
- 安卓5大布局
- 安装Oracle SOA 12c Suit Schemas
- c#自动更新+安装程序的制作
- DAO设计模式
- Git忽略文件配置
- 有关管理客户需求的一点见解
- 局域网内【1 to N】的屏幕共享_基于WPF。【不完善】
- OpenCV图像的旋转