杭电1171

Big Event in HDU

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

题意:
题意:有n样物品,每样物品价值是v,件数是m。尽量把这些物品分成两堆使得两边总价值最接近。输出分得的两堆各自的价值。
利用母函数法来解决,因为分成两堆,而两堆中较小的一堆最大为所有物品总价值量的一半,所以母函数的组合数上下就可以设置成总价值量的一半。求出所有的组合后,可以利用贪心的思想来得到答案,因为要求两堆之差尽可能小,所以就可以从总价值量的一半开始向小的方向找,找到最大的价值量,则另一堆的价值量就是总价值量-此堆的价值量。因为组合数可能较大,这里不记录组合种数,而是用一个标记来表示该数能否组合出即可。

不用算法1:

# include <iostream>
# include <algorithm>
using namespace std;

bool compare(int a,int b){

    return a<b; 
    /* < 升序 默认 > 降序 */
}
int a[10000];
int main(){

    int n,v,m,i,j,k;
    int sum;

    while(cin>>n,n>=0){

        sum = 0;
        int cnt = 0;
        while(n--){
            cin>>v>>m;  
            sum+=v*m;
            while(m--){//获取m个v 
            a[cnt++] = v;
            }
        }

        sort(a,a+cnt,compare);

        int helf = sum/2;//取中间的数
        int num = 0;
        for(i=cnt-1;i>=0;i--){
            if(num+a[i]>helf){
                continue;
            }
            num+=a[i];//num<=helf 
        } 
        cout<<sum-num<<" "<<num<<endl;
    }
    return 0;
}

解法2:

 # include <iostream>
 # include <cstring>
 # include <cstdio>
 using namespace std;

 # define MAX 250009
 int c1[MAX],c2[MAX];


 int main(){

    int n,i,j,k;
    int value[66],mount[66];
    int sum = 0;
     while(cin>>n,n>=0){
        sum = 0;

        memset(value,0,sizeof(value));
        memset(mount,0,sizeof(mount));

         for(i=0;i<n;i++){
            cin>>value[i]>>mount[i];
            sum+=value[i]*mount[i]; 
        }

        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));

        //对第一项进行初始化 
        for(i=0;i<=mount[0]*value[0];i+=value[0]){
            c1[i] = 1;
        }
        int len =  mount[0]*value[0];
        for(i=1;i<n;i++){

            for(j=0;j<=len;j++){//对mount[0]*value[0]的理解 
                for(k=0;k<=mount[i]*value[i];k+=value[i]){
                    c2[j+k] += c1[j];
                }
            }

            len += mount[i]*value[i];
            for(k=0;k<=len;k++){
                c1[k] = c2[k];
                c2[k] = 0;
            }
        } 

        for(i=sum/2;i>=0;i--){
            if(c1[i]!=0){
                printf("%d %d\n",sum-i,i);
                break;
            }
        }

    }


    return 0;
 }




你可能感兴趣的:(杭电,母函数)