题意

给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分。

思路

背包的思路显然是用一半总价值当作背包容量。

生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+...+x^{num[i]*w[i]}$的多项式,找到离$sum/2$最近的就完事。

代码

#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl; using namespace std; const int N = 300000 + 5; int n, w[N], num[N], c[2][N * 10]; int main() {
while(~scanf("%d", &n) && n > 0) {
memset(c, 0, sizeof c);
int sum = 0, ysum = 0;;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &w[i], &num[i]);
ysum += w[i] * num[i];
sum += w[i] * num[i];
}
sum /= 2;
for(int i = 0; i * w[1] <= sum && i <= num[1]; i++) c[1][i * w[1]] = 1;
for(int i = 2; i <= n; i++) {
for(int j = 0; j <= sum; j++) {
if(!c[1 - (i & 1)][j]) continue;
for(int k = 0; k <= w[i] * num[i] && j + k <= sum; k += w[i]) c[i & 1][j + k] += c[1 - (i & 1)][j];
}
for(int j = 0; j <= sum; j++) c[1 - (i & 1)][j] = 0;
}
int tmp = sum;
while(!c[n & 1][tmp]) tmp--;
printf("%d %d\n", max(tmp, ysum - tmp), min(tmp, ysum - tmp));
}
return 0;
}

  

HDU-1171 Big Event in HDU(生成函数/背包dp)的更多相关文章

  1. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  2. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  4. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  6. HDU - 1171 Big Event in HDU 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  7. 题解报告:hdu 1171 Big Event in HDU(多重背包)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  8. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

  9. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. HDU 1171 Big Event in HDU (动态规划、01背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. Fuck me

    know how to deal with \(\sum_i\binom{n}{i}a^i\)??

  2. 制作自己的Pod库(公有/私有)

    https://www.jianshu.com/p/ece0b5721461 2018.04.12 16:43* 字数 1168 阅读 244评论 0喜欢 1 目的:1.管理自己常用的类:2.组件化开 ...

  3. JQuery td内容获取,修改

    业务需求:获取某个表格中每一行第四个td内容,并根据内容为当前td重新赋值 $(".listtable.table.table-striped.table-bordered.table-ho ...

  4. 面试题(转载csdn)

    转自https://blog.csdn.net/linzhiqiang0316/article/details/80473906 相关概念 面向对象的三个特征 封装,继承,多态,这个应该是人人皆知,有 ...

  5. MDS

    转载:https://blog.csdn.net/victoriaw/article/details/78500894 多维缩放(Multidimensional Scaling, MDS)是一组对象 ...

  6. IFE第一天

    我也不知道自己到底能坚持多少天,希望66天可以坚持下来,flag在此. 第一天的知识大概就是了解一些基本概念. Web: 基于HTTP协议,利用浏览器访问网站. HTML 大概就是告诉浏览器我有一个什 ...

  7. git使用kdiff3合并乱码问题

    https://blog.csdn.net/u011008029/article/details/72644515 在合并代码过程中发现kdiff打开的文件都是乱码,解决方案如下: 第一步:点击Set ...

  8. Python 实现 Html 转 Markdown(支持 MathJax 数学公式)

    因为需要转 html 到 markdown,找了个 python 的库,该库主要是利用正则表达式实现将 Html 转为 Markdown. 数学公式需要自己修改代码来处理. 我 fork 的项目地址: ...

  9. Custom DNS on Ubuntu 18.04LTS server

    1. Edit resolved config file nano /etc/systemd/resolved.conf 2. Replace #DNS into DNS DNS=9.9.9.9 1. ...

  10. 如何对mRemoteNG在进行Linux终端访问时自定义配色

    Its not that easy to config mRemoteNG custom color themes, not like XShell which is really convinent ...