题意

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

思路

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

生成函数则是构造形如$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. 如何查看linux中文件打开情况

    前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. linux下有哪些文件 在介绍lsof命 ...

  2. 修改rpm中的文件重新打包

    1.安装rpmrebuild 和安装rpmbuild rpmrebuild下载链接:https://sourceforge.net/projects/rpmrebuild/files/rpmrebui ...

  3. 在windows系统下安装linux虚拟机(VMware)

    一.下载Vmware安装包(此处我安装的是VMware-workstation-full-14.1.3) 链接: https://pan.baidu.com/s/12xT1JaA7eheEgFfM-2 ...

  4. Luogu4655 [CEOI2017]Building Bridges

    Luogu4655 [CEOI2017]Building Bridges 有 \(n\) 根柱子依次排列,每根柱子都有一个高度.第 \(i\) 根柱子的高度为 \(h_i\) . 现在想要建造若干座桥 ...

  5. CentOS7.x编译安装zabbix4.0

    编译安装zabbix Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. Zabbix是一款能够监控各种网络参数以及服务器健康性和完整性的软件.Zabbix使用灵活的通知机制,允许用户 ...

  6. Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous

    今天使用JDBC来操作Hive时,首先启动了hive远程服务模式:hiveserver2 &(表示后台运行),然后到eclipse中运行程序时出现错误: java.sql.SQLExcepti ...

  7. 《你必须掌握的Entity Framework 6.x与Core 2.0》勘误

    第5章 5.1.1----致谢网友[宪煌] public virtual ICollection Post {get;set;} 修改为 public virtual ICollection<P ...

  8. 【动态规划】 EditDistance

    思路参考: https://www.cnblogs.com/littlepanpc/p/7895810.html 代码参考:https://leetcode.com/problems/edit-dis ...

  9. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  10. 自己常用易忘的CSS样式

    鼠标小手:   cursor:pointer 点击边框消失:outline:none; ul li下划线以及点消失: list-style-type:none; span 超出内容为...:overf ...