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. Swift 学习笔记 (继承)

    一个类可以从另一个类继承方法.属性和其他的特性.当一个类从另一个类继承的时候,继承的类就是所谓的子类,而这个类继承的类被称为父类. 在 Swift 中类可以调用和访问属于它们父类的方法.属性和下标脚本 ...

  2. HTML 学习笔记 JQuery(animation)

    动画效果也是JQuery库吸引人的地方,通过JQuery的动画方法,能够轻松的为网页天假非常紧菜的视觉效果. show()方法和hide()方法 show()方法和hide()方法是JQuery中最基 ...

  3. 线程池ThreadPoolExcecutor介绍

    线程池ThreadPoolExecutor 使用Executors和ThreadPoolExecutor 并发新特性—Executor 框架与线程池

  4. Java对象 的创建与构造方法

    一.创建对象的四种方法: a. new语句: b. 利用反射,调用描述类的Class对象的newInstance()实例方法: c. 调用对象的clone(): d. 反序列化: 其中new 和 ne ...

  5. C++ 结构体多元素sort排序调用时的写法

    //总结一下,结构体数据排序的快速写法 //以后在遇到需要写的时候,不要迟疑快速写完 struct node { int u, v, w; }a[10000]; //假设该结构体有3个元素 //现在仅 ...

  6. Codeforces Round #461 (Div. 2) B C D

    题目链接:http://codeforces.com/contest/922 B. Magic Forest time limit per test 1 second memory limit per ...

  7. 【LeetCode】求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...

  8. Discuz X3游客看小图功能导致文字内容隐藏的【修复方法】

    如果帖子内容以图片开始,并且开启了游客看小图功能,那么图片下面的文字也会被隐藏,这是不科学的(在图片上边的文字不会被隐藏)查看DZ源代码,发现是程序猿疏忽漏掉了</div> 下边1314学 ...

  9. <tx:advice/> 有关的设置

    将描述通过 <tx:advice/> 标签来指定不同的事务性设置.默认的 <tx:advice/> 设置如下: 事务传播设置是 REQUIRED 隔离级别是 DEFAULT 事 ...

  10. POJ 2409 Let it Bead:置换群 Polya定理

    题目链接:http://poj.org/problem?id=2409 题意: 有一串n个珠子穿起来的项链,你有k种颜色来给每一个珠子染色. 问你染色后有多少种不同的项链. 注:“不同”的概念是指无论 ...