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. 分布式PostGIS系列【1】— 概述

    一.Postgresql 分布式技术简介 二.分布式技术相关概念 三.分布式 Postgresql解决方案 四.相关资料与参考文献

  2. iOS进阶——App生命周期

    State Description Not running The app has not been launched or was running but was terminated by the ...

  3. javascript-对象的创建(一)

    <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF ...

  4. Cookie和Seesion的区别

    一.Cookie对象: 1.Cookie是由网络服务器发送出来,存在在浏览器上,它是个存储在浏览器目录中的文本文件.当浏览该cookie对应的站点时,cookie作为http头部文件的一部分在浏览器和 ...

  5. Uploadify 3.2 not work in IE10

    uploadify版本:Uploadify Version 3.2.1 現象:在IE10下uploadify上傳按鈕點擊失效 解決方案:替換uploadify下的js引用包 替換文件下載地址: 下载地 ...

  6. 条形码Code128源代码

    public class Code128 { private DataTable m_Code128 = new DataTable(); ; /// <summary> /// 高度 / ...

  7. MoneyUtil

    public class MoneyUtil {       private final static String[] CN_Digits = { "零", "壹&qu ...

  8. discuz x3插件开发傻瓜图文教程,用demo说话

    此demo功能是在模板footer部位插入一段javascript代码,这段代码可以是alert提示,也可以是加载广告等等. 第一步: 在config\config_global.php 文件里设置$ ...

  9. Dapper.ColumnMapper 的使用

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  10. (转载)根据数据字典表定义的表结构,生成创建表的SQL语句

    <来源网址:http://www.delphifans.com/infoview/Article_221.html>根据数据字典表定义的表结构,生成创建表的SQL语句 //1.  类名:T ...