您的位置:首页 > 其它

Big Event in HDU (多重背包)

2012-08-01 16:36 337 查看

Big Event in HDU

Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 7
[align=left]Problem Description[/align]
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).

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
10 1
20 1
3
10 1
20 2
30 1
-1


[align=left]Sample Output[/align]

20 10
40 40


[align=left]Author[/align]
lcy

题目大意:给定几组测试n,每组数据包含两个数(a,b),a代表价值,b代表数量
尽量平分总价值,输出两个数、c,d,使得c>b,且c-b的值最小
#include<cstdio>
#include<cstring>
#define max(a,b) (a>b?a:b)
#define M 51
int main()
{
int n,sum,i,j,k,divsum;
int value[M],num[M],dp[250010];
while(scanf("%d",&n),n>-1)//此处n>-1此条件很重要
{
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&value[i],&num[i]);
sum+=value[i]*num[i];
}
divsum=sum/2;
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
for(j=1;j<=num[i];j++)
for(k=divsum;k>=value[i];k--)
dp[k]=max(dp[k],dp[k-value[i]]+value[i]);
printf("%d %d\n",sum-dp[divsum],dp[divsum]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: