现在的位置: 首页 > 综合 > 正文

Hdu 1171 Big Event in HDU

2018年01月15日 ⁄ 综合 ⁄ 共 1998字 ⁄ 字号 评论关闭

Big Event in HDU

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

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<N<1000) kinds of facilities (different value, different kinds).
 

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<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding
number of the facilities) each. You can assume that all V are different.
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
 

Author
lcy

题目意思应该是将物品分成2类,使2类尽量相等,且第一类大于第2类。

解题思路:

求出用物品组合出来的能小于等于所有物品价值的一半且最接近其的值x,然后输出所有物品总值减去xx

2有两种方法,母函数或则用多重背包都可以解出来,且解法很类似,其实自我感觉现在所遇到的能用母函数解的题目,用背包也一般能解出来,且自己对背包更加熟悉,所以一般不会用母函数,母函数解法如下(现在到练习用母函数0.0):

#include<stdio.h>

int c1[250001],c2[250001];
int w[51],v[51];
int main(void)
{
    int n;
//    freopen("d:\\in.txt","r",stdin);
    while(scanf("%d",&n)&&n>=0)
    {
        int s=0,i=1;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&w[i],&v[i]);
            s+=w[i]*v[i];
        }
        int tmp=s/2;
        for(i=0;i<=tmp;i++)
        {
            c1[i]=0;
            c2[i]=0;
        }
        c1[0]=1;
        for(i=1;i<=v[1] && i*w[1]<=tmp;i++)
        {
            c1[i*w[1]]=1;
//            printf("%d\n",i*w[1]);
        }
        int m,j,k;
        for(i=2;i<=n;i++)
        {
            for(j=0;j<=tmp;j++)
            {
                for(k=w[i],m=1;k+j<=tmp && m<=v[i];m++,k+=w[i])
                {
                    if(c1[j])
                        c2[j+k]=1;
                }
                if(c1[j])
                    c2[j]=1;
            }
            for(j=0;j<=tmp;j++)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        for(i=tmp;i>=0;i--)
        {
            if(c1[i])
                break;
        }
        printf("%d %d\n",s-i,i);
    }
    return 0;
}

抱歉!评论已关闭.