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); } }
相关文章推荐
- eclipse提示错误“...armeabi-v7a\devices.xml ...发现了以元素 'd:skin' 开头的无效内容...”
- BestCoder Round #69-Baby Ming and Weight lifting(暴力)
- 打印n对括号的全部n对有效组合
- LaTex 单栏样式模板中插入双栏表格
- java2 -宏观了解
- How does Java HashMap or LinkedHahsMap handles collisions?
- 二叉树递归遍历与非递归遍历的栈空间使用
- Android ActionBar中CustomView的显示
- Caused by: java.io.IOException: Filesystem closed的处理
- oracle数据库用户创建及授权 导入 导出 删除用户 表空间
- android系列学习:tab切换,fragment中嵌套listview,listview自定义item,优化以及onclick
- 为什么要用Android Studio?
- Win10 UWP系列:关于错误 0x80073CF9及一个小bug的解决
- css学习笔记20160120链接列表表格
- 人脸识别起步
- 网络爬虫
- android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow
- git中本地与远程库的关联与取消
- servlet--转向forward与重定向
- springmvc(八)controller方法的返回值