Problem S

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

Input
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
A test case starting with a negative integer terminates input and this test case is not to be processed.

Output
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.

Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1

Sample Output
20 10
40 40
题意:给你n中给定数量的不同价值的东西,问怎么样分才能使得两堆的差最小;
解题思路:动态规划中平分东西问题有一个模板:将它所给出的n个东向西加起来sum,将sum/2当作体积,求出在sum/2下的最大值,sum-dp[sum/2];
感悟:01背包问题不能看的太简单,其衍生出来的东西太多了;
代码:
#include
#include
#include
#define maxn 255555
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,v,f[maxn],dp[maxn];
    while(~scanf("%d",&n),n>0)
    {
        memset(dp,0,sizeof dp);
        int d=0,sum=0;
        for(int l=0;l
        {
            scanf("%d%d",&v,&m);
            for(int i=0;i
            {
                f[d++]=v;
                sum+=v;
            }//将数据记录到数组,并且求出虽多的价值数
        }
        for(int i=0;i
            for(int j=sum/2;j>=f[i];j--)
        {
            dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
        }
        printf("%d %d ",sum-dp[sum/2],dp[sum/2]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781549.html