Big Event in HDU

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

Total Submission(s): 23728    Accepted Submission(s): 8363

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
/*
母函数解法:将能筹成的标记成1,找最接近sum/2的就能够
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::max;
const int MAX=52;
bool c1[MAX*MAX*100];//c2[MAX*MAX*100];
int main(){
int N;
int val[MAX],Count[MAX];
while(scanf("%d",&N),N>0){
int sum=0;
memset(val,0,sizeof(val));
memset(Count,0,sizeof(Count));
memset(c1,0,sizeof(c1));
for(int i=1;i<=N;i++){
scanf("%d%d",&val[i],&Count[i]);
sum+=val[i]*Count[i];
}
c1[0]=1;
int Limit=sum>>1;
for(int i=1;i<=N;i++){
for(int j=0;j<=Limit;j++){
for(int k=0;j+k<=Limit&&k<=val[i]*Count[i];k+=val[i])
c1[k+j]=(c1[k+j]==1?c1[k+j]:c1[j]);//注意此处不能直接等于,防止出现 50 30 20 ,v[20]=0;的情况,是能够筹成50的
}
/*
for(int j=0;j<=Limit;j++){
c1=c2[j];
c2[j]=0;
}*/
}
int k=Limit;
while(1){
if(c1[k]){
printf("%d %d\n",sum-k,k);
break;
}
k--;
}
}
return 0;
}
/*
多重背包解法:
将体积和价值都看做 价值,上限也是 sum/2,找最大值,用sum-d[sum/2]便是较大的那一个
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using std::max;
const int MAX=51;
int sum,Count,N;
int dp[MAX*MAX*100];
int v[MAX],w[MAX],c[MAX];
int V[MAX],W[MAX];
void Div(){//多重背包分解
Count=0;
for(int i=0;i<N;i++){
for(int j=1;j<=c[i];j<<=1){
W[Count++]=w[i]*j;
c[i]-=j;
}
if(c[i]>0){
W[Count++]=w[i]*c[i];
}
}
}
int main(){
while(scanf("%d",&N),N>0){
sum=0;
memset(w,0,sizeof(w));
memset(c,0,sizeof(c));
memset(W,0,sizeof(W));
memset(dp,0,sizeof(dp));
for(int i=0;i<N;i++){
scanf("%d%d",&w[i],&c[i]);
sum+=w[i]*c[i];
}
int upLimit=sum>>1;
Div();
for(int i=0;i<Count;i++){
for(int j=upLimit;j>=W[i];j--){
dp[j]=max(dp[j],dp[j-W[i]]+W[i]);
}
}
printf("%d %d\n",sum-dp[upLimit],dp[upLimit]);
}
return 0;
}

杭电1171 Big Event in HDU(母函数+多重背包解法)的更多相关文章

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

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

  2. 题解报告:hdu 1171 Big Event in HDU(多重背包)

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

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

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  4. hdu 1171 Big Event in HDU(多重背包+二进制优化)

    题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...

  5. Big Event in HDU(多重背包套用模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...

  6. HDU 1171 Big Event in HDU 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  7. 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 ...

  8. HDU1171--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(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:给出一系列的价值,需要平分,并且尽量接近. 思路:0—1背包问题. 0-1背包问题也就是有n种物品且 ...

随机推荐

  1. swift 笔记2

    swift交流群:342581988,欢迎增加. 今天真郁闷啊,把mac升级到10.10了.如今好了,曾经的程序都跑不了了.哎,不说了,让我郁闷会再. 说说条件推断吧,事实上这些基本的语法大家都知道肯 ...

  2. loj1236(数学)

    传送门:Pairs Forming LCM 题意:题意:问符合 lcm(i,j)=n (1<=i<=j<=n,1<=n<=10^14) 的 (i,j) 有多少对. 分析: ...

  3. uva315(求割点数目)

    传送门:Network 题意:给出一张无向图,求割点的个数. 分析:模板裸题,直接上模板. #include <cstdio> #include <cstring> #incl ...

  4. 上下文菜单与TrackPopupMenu

    这算是一个演示程序吧,想不到上下文菜单也是採用ON_COMMAND宏来进行消息映射,在这里,我发现一个问题:从CWnd派生的类ON_UPDATE_COMMAND_UI_RANGE似乎没有效果,不知道应 ...

  5. 2014/08/23——OJ出现waiting...

    问题: 今天中午,裴主解决OJ他缓慢的问题后,开着.我跟着oj他递给发现了一个话题waiting该..... 和全哥.均觉得測评程序挂了.于是重新启动測系统,还waiting.....(測评系统的进程 ...

  6. 那些年踩过的坑之:first-child伪类选择器

    原文:那些年踩过的坑之:first-child伪类选择器 :first-child 选择器用于选取属于其父元素的首个子元素的指定选择器.——w3school 嗯,乍一看好像说的不是很明白,因此这个选择 ...

  7. Struts2 学习第一步准备工作

    第一步:安装下载MyEclispe10 对于MyEclispe的下载安装就不再详述了. 第二步:下载Struts-2.3.15 Struts-2.3.15下载地址: http://struts.apa ...

  8. 《SAS编程和数据挖掘商业案例》第14部分学习笔记

    继续<SAS编程与数据挖掘商业案例>学习笔记系列,本次重点:经常使用全程语句 所谓全程语句.是指能够用在不论什么地方的sas语句,既能够用在data数据步语句里面,也能够用在proc过程步 ...

  9. error C2471: 无法更新程序数据库

    这段时间在使用VS做一个项目.在使用过程中,今天遇到了一个问题,也就是题目所说的那样: error C2471: 无法更新程序数据库.之后在网上搜了一下,得到了两种解决方案,两种方案分别如下: (一) ...

  10. 从零開始学android&lt;ImageSwitcher图片切换组件.二十六.&gt;

    ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...