B - Big Event in HDU

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 多重背包问题,可以将问题转化成01背包。两种思路:将多个相同物品拆分成一个一个价值相同的不同物品;也可以在01背包递推时加一层物品个数的循环。
注意这题的坑点!是以一个负整数作为结束,不要想当然以为是-1。因为这个TLE了好久。。以后要认真读题
ps:negative integer负整数 positive integer正整数
//第一种写法,耗时1248ms
#include<stdio.h>
#include<string.h> int f[],a[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,V,sum,c,x,y,i,j;
while(scanf("%d",&n)&&n>=){
memset(f,,sizeof(f));
memset(a,,sizeof(a));
sum=;c=;
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
while(y--){
a[++c]=x;
sum+=x;
}
}
V=sum/;
for(i=;i<=c;i++){
for(j=V;j>=a[i];j--){
f[j]=max(f[j],f[j-a[i]]+a[i]);
}
}
printf("%d %d\n",sum-f[V],f[V]);
}
return ;
}
//第二种写法,耗时811ms
#include<stdio.h>
#include<string.h> int f[],a[],b[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,V,sum,i,j,k;
while(scanf("%d",&n)&&n>=){
memset(f,,sizeof(f));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
sum=;
for(i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
sum+=a[i]*b[i];
}
V=sum/;
for(i=;i<=n;i++){
for(k=;k<=b[i];k++){
for(j=V;j>=;j--){
if(j-a[i]>=){
f[j]=max(f[j],f[j-a[i]]+a[i]);
}
}
}
}
printf("%d %d\n",sum-f[V],f[V]);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

  7. HDU 1171 Big Event in HDU dp背包

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

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

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

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

随机推荐

  1. 九度OJ 1149:子串计算 (计数、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1026 解决:571 题目描述: 给出一个01字符串(长度不超过100),求其每一个子串出现的次数. 输入: 输入包含多行,每行一个字符串. ...

  2. 九度OJ 1012:畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7052 解决:3034 题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工 ...

  3. 九度OJ 1030:毕业bg (01背包、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1814 解决:798 题目描述:     每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为"bg" ...

  4. 我的Java开发学习之旅------>求N内所有的素数

    一.素数的概念 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数 ...

  5. Thinkphp2.2 config.inc.php常用配置

    CHECK_FILE_CASE -- windows环境下面的严格检查大小写. /* 项目设定 */    'APP_DEBUG'    => false, // 是否开启调试模式    'AP ...

  6. fusioncharts 用法实例

    支持xml格式和json格式的数据. 用法很简单. 1.需要引入FusionCharts.js. 2.html中定义个id="chart"的div <div id=" ...

  7. Mac下php版本不支持imagetfftext函数问题

    brew rm freetype jpeg libpng gd zlib brew install freetype jpeg libpng gd zlib brew install php71 ht ...

  8. Ctags快速入门

    Ctags快速入门 在vim下阅读代码,特别是阅读不熟悉的代码时,ctags是一个提高效率的强大的工具. 1. ctags是什么? ctags可以将代码中的函数.方法.类.变量和其他的标识符进行索引, ...

  9. VC DLL方法的__declspec导入导出

    https://msdn.microsoft.com/zh-cn/library/a90k134d.aspx https://msdn.microsoft.com/zh-cn/library/ms23 ...

  10. <密码学入门>关于RSA算法的加密解密及代码实现

    RSA算法 是一种公钥加密算法,RSA算法相比别的算法思路非常清晰,但是想要破解的难度非常大.RSA算法基于一个非常简单的数论事实:两个素数相乘得到一个大数很容易,但是由一个大数分解为两个素数相乘却非 ...