HDU 1171 Big Event in HDU (多重背包)
来源:互联网 发布:java往word中写入文本 编辑:程序博客网 时间:2024/06/16 06:17
Problem Description
两堆物品,a[i]物品有b[i]件价值为c[i],求划分为两堆时各个堆的价值。
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40
Solution
多重背包转换为01背包和完全背包
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 300010;int dp[N];int v[110], num[110];int n;void ZeroOnePack(int w, int v, int W){ for (int i = W; i >= w; i--) dp[i] = max(dp[i], dp[i - w] + v);}void CompletePack(int w, int v, int W){ for (int i = w; i <= W; i++) dp[i] = max(dp[i], dp[i - w] + v);}void MultiplePack(int w, int v, int num, int W){ if (w * num >= W) CompletePack(w, v, W); else { int k = 1; while (k <= num) { ZeroOnePack(k * w, k * v, W); num -= k; k *= 2; } ZeroOnePack(num * w, num * v, W); }}int main(){ while (scanf("%d", &n), n >= 0) { int W = 0; for (int i = 1; i <= n; i++) scanf("%d%d", &v[i], &num[i]), W += v[i] * num[i]; memset(dp, 0, sizeof dp); for (int i = 1; i <= n; i++) MultiplePack(v[i], v[i], num[i], W / 2); int a = dp[W / 2], b = W - a; if (a < b) swap(a, b); printf("%d %d\n", a, b); } return 0;}
阅读全文
0 0
- hdu 1171 Big Event in HDU(01背包&多重背包)
- HDU Big Event in HDU - 多重背包
- hdu 1171多重背包Big Event in HDU
- hdu 1171 Big Event in HDU 多重背包
- hdu 1171 Big Event in HDU(多重背包可行性)
- HDU 1171 Big Event in HDU(多重背包)
- Hdu 1171 Big Event in HDU (多重背包)
- HDU 1171 Big Event in HDU(多重背包)
- hdu 1171 Big Event in HDU(多重背包)
- hdu 1171 Big Event in HDU 多重背包基础题
- hdu 1171 Big Event in HDU (多重背包)
- hdu 1171 Big Event in HDU 多重背包
- HDU 1171 Big Event in HDU (多重背包)
- hdu 1171 Big Event in HDU 多重背包
- hdu 1171 Big Event in HDU --- 多重背包
- 多重背包hdu 1171Big Event in HDU
- HDU 1171 Big Event in HDU 动态规划 多重背包
- hdu 1171 Big Event in HDU 多重背包
- Ubuntu14.04下tftp安装,并与开发板通信。
- Java引用类型
- C#应用 uploadify 上传前判断文件是否存在
- Struts2_生成验证码实例
- jQuery 判断 CheckBox是否被选中
- HDU 1171 Big Event in HDU (多重背包)
- Jboss SpringMVC调用WebService报错:setProperty must be overridden by all subclasses of SOAPMessage
- listview 修改当前item的子控件的属性
- 微信支付-商户后台(3)
- python:beautifulSoup学习(来自学习资料)
- 网页打开速度的心理学
- C++函数模板、类模板
- mybatis进阶(3)--一对一查询(使用resultMap)
- 《程序员健康指南》书摘