zoukankan      html  css  js  c++  java
  • hdu 6092 Rikka with Subset(逆向01背包+思维)

    Rikka with Subset

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1122    Accepted Submission(s): 541

    Problem Description
    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

    Yuta has n positive A1An and their sum is m. Then for each subset S of A, Yuta calculates the sum of S

    Now, Yuta has got 2n numbers between [0,m]. For each i[0,m], he counts the number of is he got as Bi.

    Yuta shows Rikka the array Bi and he wants Rikka to restore A1An.

    It is too difficult for Rikka. Can you help her?  
     
    Input
    The first line contains a number t(1t70), the number of the testcases. 

    For each testcase, the first line contains two numbers n,m(1n50,1m104).

    The second line contains m+1 numbers B0Bm(0Bi2n).
     
    Output
    For each testcase, print a single line with n numbers A1An.

    It is guaranteed that there exists at least one solution. And if there are different solutions, print the lexicographic minimum one.
     
    Sample Input
    2
    2 3
    1 1 1 1
    3 3
    1 3 3 1
     
    Sample Output
    1 2
    1 1 1
     
    Hint
    In the first sample, $A$ is $[1,2]$. $A$ has four subsets $[],[1],[2],[1,2]$ and the sums of each subset are $0,1,2,3$. So $B=[1,1,1,1]$
    Source
    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  6095 6094 6093 6092 6091 
     
     
    题目大意:
    有一个数列 a[] ,长度(n<=50)。b[i] 表示元素和为 i 的集合个数。给你一个数列 b[] ,长度(m<=10000),让你求 a[],并按照其字典序最小输出。
    题解:
    网上有说是01背包,然而我也不知道自己写了点什么,大概是01背包吧
    正常的背包:告诉你这个重量的物品个数,让你装包
    逆向背包:这题告诉你,得到的这个重量有多少种,求各个重量的物品个数
     
    官方题解:

    1008 Rikka with Subset

    签到题,大致的思想就是反过来的背包。

    如果 Bi​​ 是 B 数组中除了 B0​​ 以外第一个值不为 0 的位置,那么显然 i 就是 中的最小数。

    现在需要求出删掉 i 后的 B 数组,过程大概是反向的背包,即从小到大让 Bj-=B​(ji)​​。

    时间复杂度 O(nm)。

    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<cmath>
    #include<string>
    #include<map>
    #include<vector>
    using namespace std;
    int t,k;
    int  n,m;
    int bb[10005],b[10005],a[10005];
    int main()
    {
        scanf("%d",&t);
    
        while(t--)
        {
           scanf("%d%d",&n,&m);
           memset(a,0,sizeof(a));//a[i]表示第i个数字的个数
           memset(bb,0,sizeof(bb));//bb[k]表示当1~k-1中数字个数确定后,凑到和为k的种数,不够就表示,需要单独k这个数字来凑
    
           for(int i=0;i<=m;i++)
               scanf("%d",&b[i]);
    
           k=1; bb[0]=1;
           while(k<=m)
           {
               a[k]=b[k]-bb[k]; //这就是因为1~k-1个数确定后,能凑到和为k的种数,不够的说明a序列中有b[k]-bb[k]个数的k
    
                for(int j=1;j<=a[k];j++)
                {
                    for(int i=m;i>=k;i--) //反着来,避免已经加到结果里的数字再加一遍,这里有01背包的感觉
                       bb[i]+=bb[i-k];
                }
               k++;
           }
    
           int tot=0;  //输出
           for(int i=1;i<=m;i++)
              for(int j=1;j<=a[i];j++)
               {
                   if (tot++) printf(" ");
                   printf("%d",i);
               }
           printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    [背包问题][二进制优化] Jzoj P4224 食物
    [并查集][排序] Jzoj P4223 旅游
    [哈夫曼树][优先队列] Bzoj P4198 荷马史诗
    [hash][差分][虚树] Jzoj P6011 天天爱跑步
    [dp] Jzoj P6012 荷马史诗
    [dp][递归] Jzoj P4211 送你一棵圣诞树
    [数学] Jzoj P3912 超氧化钾
    堆学习笔记(未完待续)(洛谷p1090合并果子)
    [AC自动机]luogu P2444 病毒
    [概率期望][DP]luogu P3830 随机树
  • 原文地址:https://www.cnblogs.com/stepping/p/7324970.html
Copyright © 2011-2022 走看看