HDU1171——Big Event in HDU(母函数)
Big Event in HDU
Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40
题目大意:
有两个院系,争夺财产。输入一个T,表示有T种财产。 下面T行(x,y) 表示价值为x的财产有y个。
两个院系获得的总价值要尽量相同,并且第一个院系要大于等于第二个。
解题思路:
母函数问题。用母函数将可能组成的各种情况算出来。设价值总量为sum,
则从sum/2开始向小里面找,找到的第一个i存在,那么sum-i,i(找的是小的,不用单独判断sum奇偶)就是最接近的二个解了。
Code:
#include<stdio.h>
#define MAXN 300000
int c1[MAXN+],c2[MAXN+];
int cnt[MAXN+],weight[MAXN+];
int T;
void init(int sum)
{
int n=sum/;
int i,j,k,z;
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
for (z=,i=; z<=cnt[]&&i<=n; i+=weight[],z++)
c1[i]=;
for (i=; i<=T; i++)
{
for (j=; j<=n; j++)
for (z=,k=; k+j<=n&&z<=cnt[i]; k+=weight[i],z++)
c2[j+k]+=c1[j];
for (j=; j<=n; j++)
{
c1[j]=c2[j];
c2[j]=;
}
}
}
int main()
{
int i;
int sum=;
while (scanf("%d",&T)!=EOF)
{
if (T<) break;
sum=;
for (i=; i<=T; i++)
{
scanf("%d %d",&weight[i],&cnt[i]);
sum+=weight[i]*cnt[i];
}
init(sum);
for (i=sum/; i>=; i--)
if (c1[i])
{
printf("%d %d\n",sum-i,i);
break;
}
}
return ;
}
HDU1171——Big Event in HDU(母函数)的更多相关文章
- Big Event in HDU (母函数, 玄学AC)
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't k ...
- HDU-1171 Big Event in HDU
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- hdu1171 Big Event in HDU 01-背包
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 Problem ...
- hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory ...
- hdu1171 Big Event in HDU(多重背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...
- HDU-1171 Big Event in HDU(生成函数/背包dp)
题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...
- Big Event in HDU(HDU1171)可用背包和母函数求解
Big Event in HDU HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1 结果是:20 10.才最均匀! 三 ...
- 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- 打开shpfile,mdb,sde工作空间
打开shapefile工作空间: ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesFile.Sha ...
- 手机网站中 限制图片宽度 JS图片等比例缩放
<script type="text/javascript"> $(function () { var w = $(".content-co").w ...
- event事件:
onabort: 图像的加载被中断onblur: 元素失去焦点onchange: 域的内容被改变onclick: 当用户点击某个对象时调用的事件句柄ondblclick: 当用户双击某个对象时调用的事 ...
- CentOS 6,7最小化安装后再安装图形界面
CentOS 6.2最小化安装后再安装图形界面 在安装CentOS 6.2时发现它没有提示我要怎么安装,而是“自作主张”地给我选择了最小化安装,结果装完之后只有终端界面,因为有时候不得不用图形界面,所 ...
- Cassandra1.2文档学习(12)—— hint机制
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...
- 转发 PHP 资料(一)
WebShell隐藏思路.webshell磁盘读写动态检测.webshell沙箱动态检测(2) 作为WebShell检测.CMS修复.WebShell攻防研究学习的第二篇文章 本文旨在研究Webs ...
- htmlcleaner
String xpath = "//div"; Object[] myNodes = node.evaluateXPath(xpath); for (Object obj : my ...
- python字典根据value排序
参考: http://docs.python.org/2/howto/sorting.html http://www.cnpythoner.com/post/266.html http://ghost ...
- liger grid loadData
function fn_Search() { var beginDt = $("#txtBegin").val(); var endDt = $("#txtEnd&quo ...
- Jqplot在joomla组件中的应用
(1)在com_collect组件中采用的是ajax获取json类型的值.[http://www.jqplot.com/tests/data-renderers.php]这上边有实例. (2)在jqp ...