HDU 1171 01背包

Big Event in HDU

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

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

Author

lcy

题目意思:

给n不同种类的物品,每种物品有自己的价值w[i]和个数num[i],现把全部物品分为两部分,使得两部分总价值最接近,输出两部分总价值。

思路:

物品总价值为sum,那么每部分的总价值最接近sum/2。设一个体积为sum/2的背包,那么问题就转化为选择一些物品使得sum/2的背包中装最大价值的物品,01背包模型。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10
11 #define N 55*50*100/2
12
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16
17 int n;
18 int v[55], num[55];
19 int dp[N];
20
21 main()
22 {
23     int i, j, k;
24     while(scanf("%d",&n)==1&&n>=0){
25         int sum=0;
26         for(i=1;i<=n;i++){
27             scanf("%d %d",&v[i],&num[i]);
28             sum+=v[i]*num[i];
29         }
30         memset(dp,0,sizeof(dp));
31         for(i=1;i<=n;i++){
32             for(k=0;k<=num[i];k++){
33                 for(j=sum/2;j>=k*v[i];j--){
34                     dp[j]=max(dp[j],dp[j-v[i]*k]+v[i]*k);
35                 }
36             }
37         }
38         printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
39     }
40 }
时间: 2024-07-30 21:19:36

HDU 1171 01背包的相关文章

hdu 1171 01背包变形

背景:1Y对于背包写法,不太熟,想法也不够深,写起来,容易犯小错误. 思路:把sum/2当做背包的最大容量,求这个最大容量能够装下的最大价值,这个题的灵活之处就是把价值和体积都看做题中给的价值,那么相当于,一份体积有一份价值.所以sum/2的体积产生的价值势必小于等于sum/2,这样我们求出这个最接近sum/2的值即可. 这个题的思路比较巧妙,一是把看似两方面的问题转化为单方面要接近一半的问题,而单方面接近一半的问题又把价值既当做价值又当做体积转化为01背包问题. 学习:1.定义状态:F[j]为

poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)

题目链接: poj3211  hdu1171 这个题目比1711难处理的是字符串如何处理,所以我们要想办法,自然而然就要想到用结构体存储,所以最后将所有的衣服分组,然后将每组时间减半,看最多能装多少,最后求最大值,那么就很愉快的转化成了一个01背包问题了.... hdu1711是说两个得到的价值要尽可能的相等,所以还是把所有的价值分为两半,最后01背包,那么这个问题就得到了解决.. 题目: Washing Clothes Time Limit: 1000MS   Memory Limit: 13

Robberies hdu 2955 01背包

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10933    Accepted Submission(s): 4049 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that

hdu 1203 01背包 I need a offer

hdu 1203  01背包  I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offer的概率以及花费,给定money,求得到至少一份offer的最大概率. 简单的01背包 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 double

HDU 1171 Big Event in HDU【01背包】

题意:给出n个物品的价值和数目,将这一堆物品分给A,B,问怎样分使得两者的价值最接近,且A的要多于B 第一次做的时候,没有思路[email protected][email protected] 因为需要A,B两者最后的价值尽可能接近,那么就可以将背包的容量转化为sum/2来做,然后按照01背包的做法来做 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #de

HDU 2602 (0-1背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 35815    Accepted Submission(s): 14753 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

[HDOJ1171]Big Event in HDU(01背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 许多有价值的物品,有重复.问如何将他们分成两堆,使两堆价值之差最小. 对价值求和,转换成01背包,做一次,相当于一堆选物品使得最接近一半.然后这个结果和用价值和作差的结果就是两堆的价值,此时价值只差最小. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <c

hdoj1171 Big Event in HDU(01背包 || 多重背包)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意 老师有一个属性:价值(value).在学院里的老师共有n种价值,每一种价值value对应着m个老师,说明这m个老师的价值都为value.现在要将这些老师从人数上平分成两个院系,并且希望平分后两个院系老师的总价值A和B应尽可能地相等,求A和B的值(A>=B). 思路 由于每种老师的个数是有限的,所以使用多重背包解决.由于测试数据不是很严格,所以使用01背包也可以通过. 代码 01背包: 1

hdu 1864 01背包 最大报销额

http://acm.hdu.edu.cn/showproblem.php?pid=1864 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18562    Accepted Submission(s): 5459