Big Event in HDU

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

Total Submission(s): 24708    Accepted Submission(s): 8700

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
//背包方法:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[100000],sum,ans;
struct st
{
int v;
int m;
}data[100000];
void full(int x)
{
for(int i=data[x].v;i<=ans;i++)
dp[i]=max(dp[i],dp[i-data[x].v]+data[x].v);
}
void one(int x)
{
for(int j=1;j<=data[x].m;j++)
for(int i=ans;i>=data[x].v;i--)
dp[i]=max(dp[i],dp[i-data[x].v]+data[x].v);
}
int main()
{
int i,j,n;
while(scanf("%d",&n)&&(n>0))
{
memset(dp,0,sizeof(dp));
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&data[i].v,&data[i].m);
sum+=data[i].v*data[i].m;
}
ans=sum/2;
for(i=1;i<=n;i++)
{
if(data[i].v*data[i].m>=ans)
full(i);
else
one(i);
}
printf("%d %d\n",sum-dp[ans],dp[ans]);
}
return 0;
}
//母函数方法:
/*注意将数组a,s清零,WA了好几次,測试数据都过。。无语。
*/
#include<stdio.h>
#include<string.h>
int a[250010],s[250010];
int v[55],m[55];
int main()
{
int n,i,j,k,sum,ans;
while(scanf("%d",&n)&&n>0)
{
sum=0;
memset(s,0,sizeof(s));
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d%d",&v[i],&m[i]);
sum+=v[i]*m[i];
}
for(i=0;i<=v[1]*m[1];i+=v[1])//注意变化。
{
s[i]=1;
}
for(i=2;i<=n;i++)
{
for(j=0;j<=sum;j++)
{
for(k=0;k+j<=sum&&k<=v[i]*m[i];k+=v[i])
{
a[k+j]+=s[j];
}
}
for(k=0;k<=sum;k++)
{
s[k]=a[k];
a[k]=0;
}
}
for(i=sum/2;i>=0;i--)
{
if(s[i])
{
printf("%d %d\n",sum-i,i);
break;
}
}
}
return 0;
}


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

  1. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  2. HDU - 2604 矩阵快速幂 字符串递推 两种解法

    记dp[i]为长度i且符合题意的方案数,dp[n]就是解 符合方案的是不含fmf和fff子串的字符串 考虑如何从前面几项递推出后面第i项 (★表示存在生成的非法方案)←其实没啥用处 i=1时 m③ f ...

  3. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  4. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  5. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  6. 杭电1171 Big Event in HDU(母函数+多重背包解法)

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

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

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

  8. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  9. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

随机推荐

  1. PL/SQL批处理语句(BULK COLLECT子句和FORALL语句)

    Oracle为PL/SQL中的SQL相关功能提供了FORALL语句和BULK COLLECT子句,显著的增强了SQL相关功能.这两个语句一起被称作PL/SQL的批处理语句.Oracle为什么要提供这两 ...

  2. 使用ANR-WatchDog来检測ANR

    使用开源项目ANR-WatchDog来检測ANR.下载链接为:https://github.com/SalomonBrys/ANR-WatchDog Eclipse版本号仅仅需下载相应的jar包.在主 ...

  3. 分析javascript关闭

    1.什么是闭包 1)官方解释 一个拥有多个变量和绑定了这些变量的环境的表达式(一般是一个函数).因而这些变量也是该表达式的一部分. 我的理解:所谓的闭包就是连接函数内部和函数外部的一座桥梁.使得在外部 ...

  4. DMP文件的生成和使用

    1.生成dmp的程序 #include  <dbghelp.h> #pragma comment(lib,  "dbghelp.lib") //设置异常处理回调函数Se ...

  5. c语言实现动态指针数组Dynamic arrays

    c语言实现动态数组.其它c的数据结构实现,hashTable參考点击打开链接 treeStruct參考点击打开链接 基本原理:事先准备好一个固定长度的数组. 假设长度不够的时候.realloc一块区域 ...

  6. oracle检查点队列与增量检查点【转载】

    oracle检查点队列与增量检查点 今天是2013-09-04,这几天一直心里安顿不下来,今天还好了,可以自己安静的学习一下oracle,在此记录一下学习笔记.这篇文章我不知道在那转载的,一直都留在我 ...

  7. ASI简单实现网络编程

    使用iOS SDK中的HTTP网络请求API,相当的复杂,调用比較麻烦.ASIHTTPRequest 对CFNetwork API进行了封装.而且使用起来非常easy的一套API,在非常多比較老旧的项 ...

  8. grep与正则表达式,grep、egrep和fgrep

    grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串. ...

  9. Codeforces Round #296 (Div. 2) A B C D

    A:模拟辗转相除法时记录答案 B:3种情况:能降低2,能降低1.不能降低分别考虑清楚 C:利用一个set和一个multiset,把行列分开考虑.利用set自带的排序和查询.每次把对应的块拿出来分成两块 ...

  10. Java程序猿面试题集(181- 199)

    Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181.  182. 183. 184. 185. 186. 1 ...