zoukankan      html  css  js  c++  java
  • HDU 1171 Big Event in HDU(母函数或01背包)

    Big Event in HDU

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


    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
     
    Recommend
    We have carefully selected several similar problems for you:  2602 1203 2159 2955 2844 
     
    分析:要将所有的值分成两个相近的部分,那么总值一定能得到,如果我们能找到一个最接近它的二分之一的值能得到,那么另一部分也一定能得到
     
    母函数解法 ,表示不懂为什么最后不能直接判if(a[i]>0) 而是if(a[i])
    代码如下:
    #include<iostream>
    #include <cstring>
    using namespace std;
    const int  M=50*50*100+10;   //M为(x的最大指数+1)
    #define MAXN 52      //MAXN为最大有多少项相乘
    typedef long long ll;
    ll a[M],b[M];//a[M]中存最终项系数;b[M]中存取中间变量;
    struct node
    {
        int val;
        int num;
    }c[MAXN];
    int main()
    {
        int m,n;
        int i,j,k,sum;
        while(scanf("%d",&n)!=EOF&&n>=0)
        {
            sum=0;
            memset(a,0,sizeof(a));
            m=n;
            for(int i=0;i<n;i++)
            {
              scanf("%d%d",&c[i].val,&c[i].num);
              sum+=c[i].val*c[i].num;
            }
            //可以加一个跳出循环的条件
            //n为所求的指数的值: ";
            //因为只求指数为n的系数的值:所以循环只到n就结束
            for(i=0;i<=c[0].val*c[0].num;i+=c[0].val)//初始化第一个式子:(1+X^2+X^3+...) 所以将其系数分别存到a[n]
            {
                a[i]=1;
                b[i]=0;
            }
            for(i=2;i<=m;i++)//从第2项式子一直到第n项式子与原来累乘项的和继续相乘
            {
                for(j=0;j<=sum;j++)//从所累乘得到的式子中指数为0遍历到指数为n 分别与第i个多项式的每一项相乘
                     for(k=0;k+j<=sum&&k<=c[i-1].val*c[i-1].num;k+=c[i-1].val)//第i个多项式的指数从0开始,后面的每项指数依次比前面的多i,比如当i=3时,第3项的表达式为(1+x^3+x^6+x^9+……),直到所得指数的值i+j>=n退出
                     {
                         b[j+k]+=a[j];//比如前面指数为1,系数为3,即a[1]=3 的一项和下一个表达式的指数为k=3的相乘,则得到的式子的系数为,b[j+k]=b[4]+=a[1],又a[1]=3,所以指数为4的系数为b[4]=3;
                     }
                 for(j=0;j<=sum;j++)//  然后将中间变量b数组中的值依次赋给数组a,然后将数组b清零,继续接收乘以下一个表达式所得的值
                  {
                      a[j]=b[j];
                      b[j]=0;
                  }
            }
            for(int i=sum/2;i>=0;i--)
            {
              if(a[i])
              {
                  printf("%d %d
    ",sum-i,i);
                  break;
              }
            }
        }
    
        return 0;
    }
    

      01背包:

    #include <cstdio>
    #include <iostream>
    #include  <algorithm>
    #include <vector>
    #include <cstring>
    using namespace std;
    const int MAXN=50*50*100+10;
    #define INF 0x3f3f3f3f
    vector<int>V;
    int dp[MAXN];
    int n;
    int main()
    {
          int t,n,v,m,sum;
           while(scanf("%d",&n)!=EOF&&n>=0){
               sum=0;
               V.clear();
               while(n--){
               scanf("%d%d",&v,&m);
               sum+=v*m;
                for(int i=0;i<m;i++)
                V.push_back(v);
               }
               fill(dp,dp+sum+1,INF);
               dp[0]=0;
               for(int i=0;i<V.size();i++)
                  for(int j=sum;j>=V[i];j--)
                  {
                     dp[j]=min(dp[j],dp[j-V[i]]+V[i]);
                  }
               for(int i=sum/2;i>=0;i--)
               {
                  if(dp[i]<=100000000)
                  {
                    printf("%d %d
    ",sum-i,i);
                    break;
                  }
               }
           }
        return 0;
    }
  • 相关阅读:
    Qt之数据类型转换
    [leetcode] Sudoku Solver
    调用摄像头拍照实例,摇一摇实例
    html 标签
    JavaScript 常见的六种继承方式
    js的DOM的方法和属性总结
    BOM属性方法。
    DOM 元素对象的属性方法
    完整的用户代理字符串检测
    可以跨浏览器获得窗口左边,和上边的位置
  • 原文地址:https://www.cnblogs.com/a249189046/p/7529272.html
Copyright © 2011-2022 走看看