• HDOJ1709 The Balance(母函数)


    The Balance

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4327    Accepted Submission(s): 1739


    Problem Description
    Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.
     
    Input
    The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.
     
    Output
    For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.
     
    Sample Input
    3 1 2 4 3 9 2 1
     
    Sample Output
    0 2 4 5
     
    Source
     

    题意:
    给出一些砝码,可以放在天秤的两边,问有[1,sum]中有哪些重量是不可称出来的
    题解:
    母函数,这里比较特殊的一点是砝码可以放在天枰的左右两端,我们可以在c2[j+k]+=c1[j]
    后加多一句c2[abs(j-k)]+=c[j]...即可,假设原来的砝码都放在右端,则可以把新加的砝码放在左端,得到新重量。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 
     5 using namespace std;
     6 
     7 int c1[10005], c2[10005];
     8 
     9 int main()
    10 {
    11     int n, sum;
    12     int a[105];
    13     while(~scanf("%d", &n))
    14     {
    15         sum = 0;
    16         for(int i = 0; i < n; ++i)
    17         {
    18             scanf("%d", &a[i]);
    19             sum += a[i];
    20         }
    21         memset(c1, 0, (sum+1)*sizeof(int));
    22         memset(c2, 0, (sum+1)*sizeof(int));
    23         c1[0] = c1[a[0]] = 1;
    24         int end = a[0];
    25         for(int i = 2; i <= n; ++i)
    26         {
    27             for(int j = 0; j <= end; ++j)
    28             {
    29                 for(int k = 0; k <= a[i-1] && j+k <= sum; k += a[i-1])
    30                 {
    31                     c2[j+k] += c1[j];
    32                     c2[abs(j-k)] += c1[j];
    33                 }
    34             }
    35             end += a[i-1];
    36             for(int j = 0; j <= end; ++j)
    37             {
    38                 c1[j] = c2[j];
    39                 c2[j] = 0;
    40             }
    41         }
    42         int cnt = 0;
    43         for(int j = 0; j <= sum; ++j)
    44         {
    45             if(c1[j] == 0)
    46                 c2[cnt++] = j;
    47         }
    48         if(cnt == 0)
    49             printf("0\n");
    50         else
    51         {
    52             printf("%d\n", cnt);
    53             for(int i = 0; i < cnt-1; ++i)
    54                 printf("%d ", c2[i]);
    55             printf("%d\n", c2[cnt-1]);
    56         }
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    c++ accumulate
    Croc Champ 2013 Round 2 (Div. 2 Edition) B. Ksusha the Squirrel
    ural 1017. Staircases
    ural 1012Kbased Numbers. Version 2 1013. Kbased Numbers. Version 3
    ural 1008. Image Encoding
    ural 1009. Kbased Numbers
    echo命令去掉换行符
    linux中shell 参数变量
    C#中可变字符串StringBuilder和String
    C#异常处理语句
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3049005.html
Copyright © 2020-2023  润新知