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. android 首开机会在数据链接图标的状态栏打开并自行消失主动

    请找到该文件ConnectivityService.java (alps\frameworks\base\services\java\com\android\server)  在connectivit ...

  2. jquery 触屏滑动+定时滚动

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. winform动态的文字效果

    效果图如下

  4. 【ios开发】Block编程

    1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...

  5. MongoDB的.Net驱动

    mongo的驱动主要使用了两个,即samus和官方驱动. 个人感觉差别不大,且官方驱动也支持了LinQ.但在使用DBRef的时候,发现samus的驱动似乎不太好用,并没有达到想要的效果,也许是我的使用 ...

  6. [转]Use the IDA and LLDB explore WebCore C + + class inheritance

    原文:http://www.phonesdevelopers.com/1781016/ The surgery class named PluginWidgetIOS use lldb can get ...

  7. python(学习之路一)

    ''' Created on 2013-5-3 @author: lixingle ''' #输出的练习 length=3 width=4; area=length*width print(area) ...

  8. 揭开redis神秘面纱

    一直听别人说NoSQL,以前一直不明白,这到底是什么东西,今天听过我们涛哥的讲解,略有小感,特此小记. NoSQL(NoSQL = Not Only SQL),意为反SQL运动,是一项全新的数据库革命 ...

  9. Cassandra

    NoSQL之Cassandra   9月初听了一个讲座,演讲者是张月同学,他给我们分享了Cassandra nosql数据库,讲得很精彩,听完之后收益良多. Cassandra是一个noSQL数据库, ...

  10. MVC 静态化的ActionFilter

    在MVC中,需要对某些页面进行静态化,用ActionFilter来做静态化,把页面存到缓存中.如下代码所示,其中Result.RenderString是扩展方法,第一次缓存的时候,Action代码会运 ...