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. Win7设置共享文件夹

    在系统中,实现共享的前提是,双方要在同一个局域网内,同一个工作组或者域内. 添加共享用户 点击确定即可,最好把共享的权限设置为只读类型,方便管理. 可以使用计算机的用户名或者IP地址来访问 如果还是不 ...

  2. L001-oldboy-mysql-dba-lesson01

          L001-oldboy-mysql-dba-lesson01 <sql应用重构>经典的书 ,思想,封顶境界! mysql下载页面: http://www.filewatcher ...

  3. 第八章 Qt GUI之对话框使用

    第八章 Qt GUI之对话框使用 对话框可以是模态(modal)的或非模态(modeless)两种.当我们在一个用户界面程序里面对一个对话框(比如选择文件对话框)的操作没有结束前,界面的其他窗口无法操 ...

  4. 重写session

    <?php ini_set('session.use_only_cookies', 0); $session_id = rand(10000, 99999); session_set_cooki ...

  5. 如何快速建立Subversion服务器

    本文拷贝自网址:http://www.subversion.org.cn/?action-viewnews-itemid-1 如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关 ...

  6. ADO和ADO.NET的区别

    1. ADO与ADO.NET简介 ADO与ADO.NET既有相似也有区别,他们都能够编写对数据库服务器中的数据进行访问和操作的应用程序,并且易于使用.高速度.低内存支出和占用磁盘空间较少,支持用于建立 ...

  7. MySQL Partition分区扫盲

    MySQL从5..3开始支持Partition,你可以使用如下命令来确认你的版本是否支持Partition: mysql> SHOW VARIABLES LIKE '%partition%'; ...

  8. 十一、mysql输入安全

    .尽量使用“绑定参数”功能,php中可用pdo进行一系列操作 .php可使用mysql_real_escape_string()函数进行输入过滤:

  9. PS 颜色表大全-CMYK颜色表(2)

    CMYK颜色表 编号 C M Y K R G B 16进制值 1 0 100 100 45 139 0 22 8B0016 2 0 100 100 25 178 0 31 B2001F 3 0 100 ...

  10. iOS 10 的适配问题-b

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ...