Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 48364 Accepted Submission(s): 16581

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

【题意】:有很多个人,其中每个人有一个自己的权值,权值一共有n种,要分配成两个部分,要尽量保证每个部分的分数尽量接近,并且第一部分的权值要大于等于第二个部分。

【分析】:总代价是背包的一半。经典的求两堆数分别求和以后的差最小,转化为01背包。首先计算总权值sum,以总权值的一半作为背包的容量,由于第二部分的分数恒小于等于总权值一半,把第二部分当作背包模型。又要保证两部分权值要尽量相等,所以把每个人的权值即当做放入背包中的物体的体积,也当做物体的价值,就可以保证第二部分的权值小于第一部分却有尽可能接近第一部分的权值。

【注意】:这道题是以负数作为输入的结束标志的,而不只局限于-1, 被惯性思维坑了。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int MAXN = 1e3 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n,m;
int a,b;
int c[maxm];
int v[maxm], w[maxm],dp[maxm];
int main()
{
int n,k,sum;
while(cin>>n, n>0){
k=sum=0;
memset(c,0,sizeof(c));
memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++){
cin>>a>>b;
while(b--){
c[k++]=a;
sum+=a;
}
} for(int i=0; i<k; i++){
for(int j=sum/2; j>=c[i]; j--){
dp[j] = max(dp[j],dp[j-c[i]]+c[i]);
}
}
cout<<sum-dp[sum/2]<<' '<<dp[sum/2]<<endl;
}
return 0;
}

HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】的更多相关文章

  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 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  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. 【01背包】HDU 1171 Big Event in HDU

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

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

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

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

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

  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. GCD那些事儿

    GCD GCD,全名Grand Central Dispatch,中文名郭草地,是基于C语言的一套多线程开发API,一听名字就是个狠角色,也是目前苹果官方推荐的多线程开发方式.可以说是使用方便,又不失 ...

  2. 剑指Offer - 九度1507 - 不用加减乘除做加法

    剑指Offer - 九度1507 - 不用加减乘除做加法2013-11-29 20:00 题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 输入: 输入可能包 ...

  3. Flash文件在asp页面无法播放,网页上面的Flash文件在火狐浏览器不播放

    第一个问题:Flash文件放到asp页面以后无法播放. 解决方法:用浏览器打开页面->F12,选择Network,如下图: 然后刷新页面,如下图: 点击左侧状态是404的文件,如图: 可以发现F ...

  4. LCS+LIS

    #include<iostream> #include<string> using namespace std; string a,b; ][]; int main() { w ...

  5. B树、B-树、B+树、B*树 红黑树

    转载自:http://blog.csdn.net/quitepig/article/details/8041308 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): ...

  6. 自动化测试环境搭建--Python及selenium

    安装pyhton 访问Python官网:http://www.python.org 下载页Windows下找到适合64位系统的版本 下载后双击安装 安装后查看计算机->属性->高级系统设置 ...

  7. UVALive 4764 简单dp水题(也可以暴力求解)

    B - Bing it Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status ...

  8. mysql备份策略

    1.备份的种类 完全备份,就是备份整个数据库对象 事务日志备份, 备份事务日志记录上一次的数据库改变 增量备份,也叫差异备份 文件备份 2.备份方式 逻辑备份, 既备份sql语句,使用mysql自带的 ...

  9. hdu 3499 Flight (最短路径)

    Flight Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  10. Codeforces Round #392(div 2) 758D (贪心)

    orz 最近被水题卡+FST,各种掉rating 题目大意 一个数s它是n进制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,将它还原成十进制 这种表示方法显然会产生多解 ...