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(母函数)的更多相关文章

  1. Big Event in HDU (母函数, 玄学AC)

    Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't k ...

  2. HDU-1171 Big Event in HDU

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  3. hdu1171 Big Event in HDU 01-背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 Problem ...

  4. 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 ...

  5. HDU 1171 Big Event in HDU 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  6. hdu1171 Big Event in HDU(多重背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...

  7. HDU-1171 Big Event in HDU(生成函数/背包dp)

    题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...

  8. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  9. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

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

随机推荐

  1. 反编译.o到.cpp

    反编译.o到.cpp 现在有很多反编译是对Java的,而.cpp的很少 因为不同的平台.cpp生成的.o是不一样的,但是Java因为有虚拟机的中间作用,生成的.o在不同平台是一样的 反编译的机理 (通 ...

  2. 增加用户为SiteCollection的管理员

    1.SiteSettings-->Site collection administrators --> 增加你需要的用户

  3. Centos6.5网络无法连接问题

    1. 先进入对应文件夹: cd /etc/sysconfig/network-scripts/ 2.获取root权限: su     然后输入root密码 3.修改ifcfg-eth0 vi ifcf ...

  4. 锋利的qjuey-ajax

    jquery 中的ajax   load方法主要获取web服务器上静态数据 1 load方法载入HTML文档 load(url [,data] [,callback]) $(function(){ $ ...

  5. Facebook SDK

    <?php session_start(); header('Content-type:text/html; charset=UTF-8'); require_once './facebook- ...

  6. Discuz 哪些文件和文件夹需要777权限

    Discuz有强大的缓存能力,当然要想建立这些缓存,就需要对需要的文件和文件夹进行读写,因此给需要的文件和文件夹777权限(Linux服务器)是必不可少的,下面就列出了需要给777权限的文件和文件夹, ...

  7. sql新感悟(where 1 = 1)

    花了好久把YII框架看完发现一本很不错的书:SQL案例解析(清华大学出版社),看到一些比较有用的东西,感觉应该把他记录下来,看了好多页发现书中一直有 where 1=1,这样的语句,查过发现“wher ...

  8. HTML中href的链接刷新页面问题

    在上一篇随笔中说到了html()方法不能一直改变标签的值的问题,当单击完成时,回调函数返回的值瞬间就没有了,今天突然想到了,我单击的是链接啊,就算链接到本界面上,也要进行刷新,页面一刷新,显示的值自然 ...

  9. ARM-Linux S5PV210 UART驱动(4)----串口驱动初始化过程

    对于S5PV210 UART驱动来说,主要关心的就是drivers/serial下的samsung.c和s5pv210.c连个文件. 由drivers/serial/Kconfig: config S ...

  10. Win32的绘图消息大集合

    AbortPath 抛弃选入指定设备场景中的所有路径.也取消目前正在进行的任何路径的创建工作AngleArc 用一个连接弧画一条线Arc 画一个圆弧BeginPath 启动一个路径分支CancelDC ...