Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8342Accepted Submission(s): 2835
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).
A test case starting with a negative integer terminates input and this test case is not to be processed.
直接把多重背包转化为01背包水过的。。
不过我也太水了。。
我的代码:
#include<stdio.h>
#include<string.h>
int a[5005],dp[250005];
int num,sum;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
int main()
{
int n,m,k,i,j;
while(scanf("%d",&n)!=EOF)
{
if(n<0)
break;
num=0,sum=0;
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
scanf("%d%d",&m,&k);
for(j=1;j<=k;j++)
a[num++]=m;
sum=sum+m*k;
}
for(i=0;i<num;i++)
for(j=sum/2;j>=a[i];j--)
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
printf("%d %d\n",max(dp[sum/2],sum-dp[sum/2]),min(dp[sum/2],sum-dp[sum/2]));
}
return 0;
}