Big Event in HDU HDU - 1171

时间:2022-06-03 14:31:25

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171

题意:给出每个物体的价值和物体的数量,如何分使得A,B所得价值最接近并且A的价值不能小于B

思路:将总和平分后,就是一道01背包题了

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 1e6;
int val[maxn];
int dp[maxn];
int main()
{
int n,i,j,a,b,l,sum;
while(~scanf("%d",&n))
{
if(n<=)break;
memset(val,,sizeof(val));
memset(dp,,sizeof(dp));
l = ;
sum = ;
for(i = ;i<n;i++)
{
scanf("%d%d",&a,&b);
while(b--)
{
val[l++] = a;
sum+=a;
}
}
for(i = ;i<l;i++)
{
for(j = sum/;j>=val[i];j--)//01背包
{
dp[j] = max(dp[j],dp[j-val[i]]+val[i]); }
}
printf("%d %d\n",sum-dp[sum/],dp[sum/]);
}
return ;
}