hdu1171Big Event in HDU(母函数或多重背包)

Big Event in HDU

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


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
母函数解法:将每一个都当一种物品,拿这些物品来拼成最大值得一半。若不存在,就往下找存在的。
//表达式(1+x^a[i])*(1+x^a[i+1]).....*/ #include <iostream> using namespace std; #include<stdlib.h> #include<stdio.h> #include <cstring> #include<string> #include<cmath> #include<algorithm> int c1[100000],c2[100000]; int a[1001][2],b[100000]; int main() {     int T,i;     while(~scanf("%d",&T))     {         if(T<0)             return 0;         int i,sum=0,j,k,q=0;         for(i=0;i<T;i++)         {             scanf("%d%d",&a[i][0],&a[i][1]);             sum+=a[i][0]*a[i][1];//计算最多到达的指数。         }         memset(c1,0,sizeof(c1));         memset(c2,0,sizeof(c2));         for(i=0;i<T;i++)//将数分离。         {             for(j=1;j<=a[i][1];j++)              {                  b[q]=a[i][0];                  q++;              }         }         c1[0]=1;         c1[b[0]]=1;         for(i=1;i<q;i++)         {             for(j=0;j<=sum/2;j++)//第二个括号最大到达的指数             {                 if(c1[j]==1)                 {                     c2[j]=1;                     c2[j+b[i]]=1;                 }             }             for(j=0;j<=sum/2;j++)             {                  c1[j]=c2[j];                  c2[j]=0;             }         }         for(i=sum/2;i>=0;i--)//找出分的最多的质量看存在不。          if(c1[i]==1)           {               printf("%d %d\n",sum-i,i);               break;           }     }     return 0; }
多重背包解法:
#include <stdio.h> #include<string.h> #include<iostream> using namespace std; #include<algorithm> int a[1000],b[1000],dp[100000]; int main() {    int n;    while(cin>>n)    {        int i,j,k,sum=0;        if(n<0)         return 0;        for(i=0;i<n;i++)        {            cin>>a[i]>>b[i];//a代表体积,b表示质量。            sum+=a[i]*b[i];        }        memset(dp,0,sizeof(dp));        //多重背包的模板        for(i=0;i<n;i++)        {            for(j=1;j<=b[i];j++)//控制数量的。            {                for(k=sum/2;k>=a[i];k--)                {                    dp[k]=max(dp[k-a[i]]+a[i],dp[k]);                }            }        }        cout<<max(dp[sum/2],sum-dp[sum/2])<<" "<<min(dp[sum/2],sum-dp[sum/2])<<endl;//找出最大值。    }     return 0; }


 

你可能感兴趣的:(+,背包,母函数)