Big Event in HDU HDU

题目链接: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<=0)break;
        memset(val,0,sizeof(val));
        memset(dp,0,sizeof(dp));
        l = 0;
        sum = 0;
        for(i = 0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            while(b--)
            {
                val[l++] = a;
                sum+=a;
            }
        }
        for(i = 0;i<l;i++)
        {
            for(j = sum/2;j>=val[i];j--)//01背包
            {
                dp[j] = max(dp[j],dp[j-val[i]]+val[i]);

            }
        }
        printf("%d %d
",sum-dp[sum/2],dp[sum/2]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kayiko/p/10776870.html