zoukankan      html  css  js  c++  java
  • HDU1171——Big Event in HDU(母函数)

    Big Event in HDU

    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

    题目大意:

        有两个院系,争夺财产。输入一个T,表示有T种财产。 下面T行(x,y) 表示价值为x的财产有y个。

        两个院系获得的总价值要尽量相同,并且第一个院系要大于等于第二个。

    解题思路:

        母函数问题。用母函数将可能组成的各种情况算出来。设价值总量为sum,

        则从sum/2开始向小里面找,找到的第一个i存在,那么sum-i,i(找的是小的,不用单独判断sum奇偶)就是最接近的二个解了。

    Code:

     1 #include<stdio.h>
     2 #define MAXN 300000
     3 int c1[MAXN+10],c2[MAXN+10];
     4 int cnt[MAXN+10],weight[MAXN+10];
     5 int T;
     6 void init(int sum)
     7 {
     8     int n=sum/2;
     9     int i,j,k,z;
    10     memset(c1,0,sizeof(c1));
    11     memset(c2,0,sizeof(c2));
    12     for (z=0,i=0; z<=cnt[1]&&i<=n; i+=weight[1],z++)
    13         c1[i]=1;
    14     for (i=2; i<=T; i++)
    15     {
    16         for (j=0; j<=n; j++)
    17             for (z=0,k=0; k+j<=n&&z<=cnt[i]; k+=weight[i],z++)
    18                 c2[j+k]+=c1[j];
    19         for (j=0; j<=n; j++)
    20         {
    21             c1[j]=c2[j];
    22             c2[j]=0;
    23         }
    24     }
    25 }
    26 int main()
    27 {
    28     int i;
    29     int sum=0;
    30     while (scanf("%d",&T)!=EOF)
    31     {
    32         if (T<0) break;
    33         sum=0;
    34         for (i=1; i<=T; i++)
    35         {
    36             scanf("%d %d",&weight[i],&cnt[i]);
    37             sum+=weight[i]*cnt[i];
    38         }
    39         init(sum);
    40         for (i=sum/2; i>=0; i--)
    41             if (c1[i])
    42             {
    43                 printf("%d %d
    ",sum-i,i);
    44                 break;
    45             }
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    各个数字类型取值范围以及推理
    进制转换原理
    位运算操作符_
    读取文件内容
    java中thread的start()和run()的区别
    二进制的负数转换
    位运算符号
    Hadoop的辉煌还能延续多久?
    Hadoop 新 MapReduce 框架 Yarn 详解
    MapReduce工作原理讲解
  • 原文地址:https://www.cnblogs.com/Enumz/p/3878390.html
Copyright © 2011-2022 走看看