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. NOIP201101&&05

    NOIP200701奖学金 难度级别:A:            运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述    某小学最近得到了一笔赞助 ...

  2. MySql中,复制旧表结构到新表

    # 创建学生表 create table student(age int,name varchar(32))engine myisam charset utf8;insert into student ...

  3. Java多线程(二) 多线程的锁机制

    当两条线程同时访问一个类的时候,可能会带来一些问题.并发线程重入可能会带来内存泄漏.程序不可控等等.不管是线程间的通讯还是线程共享数据都需要使用Java的锁机制控制并发代码产生的问题.本篇总结主要著名 ...

  4. 简单的MySQLDB类

    <?php error_reporting(E_ALL ^ E_DEPRECATED); //数据库操作类 class MySQLDB{ //属性--必要的信息 private $_host; ...

  5. 无法建立到http://localhost:6080/arcgis/manager/的连接

    安装ArcGIS server10.1后,打开管理页面提示“无法建立到http://localhost:6080/arcgis/manager/的连接” 原因是:在ArcGIS for Server ...

  6. 【原】在一般处理程序中设置session

    using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...

  7. WeX5是主要进行app开发吗?能开发微信App吗?

    WeX5是一款html5开发工具,可以进行app开发,做出各种H5 App,同样也可以进行主要运行在PC的html5产品,. WeX5开发的应用,不仅可以在微信上运行,也可以直接手机浏览器运行,或者打 ...

  8. html标准写法

    <!--doctype指定文档类型htm--> <!doctype html> <html> <header> <!--设置字符集 utf-8-- ...

  9. [PHP]set_time_limit — 设置脚本最大执行时间

    (PHP 4, PHP 5) set_time_limit — 设置脚本最大执行时间 说明 void set_time_limit ( int $seconds ) 设置允许脚本运行的时间,单位为秒. ...

  10. 3、WPF学习之-布局

    一.基础知识 1.所有WPF布局容器都派生自System.Windows.Controls.Panel抽象类的面板: 2.WPF种核心布局面板有StackPanel(栈面板).WrapPanel(环绕 ...