Problem 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

Author

lcy
 
思路是先算出总价值,然后除以二,所以A和B一定是最接近value/2的一左一右两个数,用dp[value/2]去做就是B,A=value-B
 
 #include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int a[];
int dp[]; int main()
{
int n,v,s,value,value1;
while(~scanf("%d",&n)&&n>=)
{
value=;
int j=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&v,&s);
while(s--)
{
a[j]=v;
j++;
value+=v;
}
}
value1=value;
value/=;
memset(dp,,sizeof(dp));
for(int i=;i<j;i++)
{
for(int k=value;k>=a[i];k--)
{
dp[k]=max(dp[k],dp[k-a[i]]+a[i]);
}
}
printf("%d %d\n",value1-dp[value],dp[value]);
}
return ;
}
 

【01背包】HDU 1171 Big Event in HDU的更多相关文章

  1. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  2. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

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

  3. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

  4. hdu 1171 Big Event in HDU (01背包, 母函数)

    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 (动态规划、01背包)

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

  6. HDU 1171 Big Event in HDU(01背包)

    题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...

  7. HDU 1171 Big Event in HDU【01背包】

    题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路---@_@ 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容 ...

  8. HDU 1171 Big Event in HDU (多重背包)

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

  9. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

随机推荐

  1. JS定时跳转URL并输出剩余秒数

    1. [代码][JavaScript]代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <scrip ...

  2. IS_EER分析

    下面我们就来具体分析一下这段代码,看看内核中的巧妙设计思路. 要想明白IS_ERR(),首先理解要内核空间.所有的驱动程序都是运行在内核空间,内核空间虽然很大,但总是有限的,而在这有限的空间中,其最后 ...

  3. JavaScript等比例缩放图片

    js等比例缩放图片,这个功能非常实用,当网页加载一个尺寸比较大的图片时,往往会把一个网页撑的变形,页面变得很难看,于是我们就想到了用JS去控制超出一定范围的图片,以稳定页面布局,本代码段就是完成了此功 ...

  4. OpenGL Development Cookbook chapter7部分翻译

    让我们通过以下简单步骤开始我们的配方: 1.通过读取外部的体数据文件,并通过该加载数据集数据转换成一个OpenGL纹理.也使硬件的mipmap生成.通常情况下,从使用一个横截面中获得的体积数据文件存储 ...

  5. 【推荐】30个Matlab视频教程合集(含GUI视频教程)下载

    自己收集别人网盘上存的资源,分享一下[点击文件名可得到下载地址]        Matlab 7.8 基础视频教程 实例1 数据传递和多窗口编程_avi.zip 205.11 MB   Matlab ...

  6. 基于Web的IIS管理工具

    Servant:基于Web的IIS管理工具   Servant for IIS是个管理IIS的简单.自动化的Web管理工具.安装Servant的过程很简单,只要双击批处理文件Install Serva ...

  7. Algorithm lecture

    当前标签: lecture   组合数求法讲解 BLADEVIL 2014-01-08 18:59 阅读:3 评论:0     mobius反演讲解 BLADEVIL 2014-01-08 18:13 ...

  8. C#算两个时间段相差的时间

    在数据中经常算两个时间差或者在某个时间段的内容 在数据库中设计表字段类型的时候设计为varchar类型,然后进行可以再Sql语句中书写>=或者<=这样的进行比较就可以查询出某个时间段的内容 ...

  9. Knockout 可扩展性

    你需要知道的顶级特性 Knockout 最棒的一个特点就是它的可扩展性.Knockout 存在大量的扩展点,包含大量的工具来创建我们的应用程序.许多开发者除了 Knockout 核心库之外没有使用任何 ...

  10. 【python】初识python的问题

    这两天利用晚上时间简单的了解了一下python语言,在Mac上和Windows上都安装了python,对比两个平台,还是发现在mac上体验比较好一点.安装的版本好像也不一样,语法还有点小区别.简单的对 ...