HDU1171(01背包)

Big Event in HDU

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

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

#include "cstring"
#include "cstdio"
#include "iostream"
using namespace std;
int max(int a,int b)
{
    if(a>b) return a;
    else return b;
}
int val[250002];
int dp[250002];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n>0)
    {
        int sum=0;
        int m=1;
        for(int i=1;i<=n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            while(b--)
            {
                sum+=a;
                val[m++]=a;
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<m;i++)
        {
            for(int j=sum/2;j>=val[i];j--)
            {
                dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
            }
        }
        printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
    }
}

你可能感兴趣的:(HDU1171(01背包))