hdu 6092 Rikka with Subset(逆向01背包+思维)

本文介绍了一种被称为逆向背包的问题,与传统背包问题不同,该问题需要从已知的各种组合数量反推出物品的数量。文章详细阐述了解决方案,并通过一个具体的示例展示了如何找到满足条件的最小字典序解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:http://www.cnblogs.com/stepping/p/7324970.html

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 A1−An 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 A1−An.

It is too difficult for Rikka. Can you help her?

Input
The first line contains a number t(1≤t≤70), the number of the testcases.

For each testcase, the first line contains two numbers n,m(1≤n≤50,1≤m≤104).

The second line contains m+1 numbers B0−Bm(0≤Bi≤2n).

Output
For each testcase, print a single line with n numbers A1−An.

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
2017 Multi-University Training Contest - Team 5
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
签到题,大致的思想就是反过来的背包。

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

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

时间复杂度 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("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值