hdu 6092 Rikka with Subset(逆向01背包+思维)
来源:互联网 发布:知乎国外智慧城市案例 编辑:程序博客网 时间:2024/05/17 00:11
参考: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,
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
签到题,大致的思想就是反过来的背包。
如果 Bi 是 B 数组中除了 B0 以外第一个值不为 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;}
- hdu 6092 Rikka with Subset(逆向01背包+思维)
- HDU 6092 Rikka with Subset(01背包 思维)
- hdu 6092 Rikka with Subset 逆向背包
- 【HDU 6092 Rikka with Subset】 思维 & 背包
- HDU 6092 Rikka with Subset (背包 思维)
- hdu 6092 Rikka with Subset【01背包+思维】
- HDU 6092 Rikka with Subset(背包+思维)
- hdu-6092-Rikka with Subset(思维)
- HDU 6092 Rikka with Subset(思维)
- Hdu 6092 Rikka with Subset【背包Dp】
- hdu 6092 Rikka with Subset 背包dp
- hdu 6092 Rikka with Subset(dp背包)
- HDU 6092 Rikka with Subset (2017多校5-背包)
- hdu 6092 Rikka with Subset 背包(贪心)
- HDU 6092 Rikka with Subset(完全背包)
- hdu 6092 Rikka with Subset(多重背包)
- HDU 6092 Rikka with Subset(多重背包)
- Rikka with Subset(HDU 6092)
- RabbitMQ消息队列(六):使用主题进行消息分发
- 解析IP分组
- Ubuntu16.04 install lsd-slam
- 005 爬虫之正则表达式
- [JZOJ5442]【NOIP2017提高A组冲刺11.1】荒诞([BZOJ3060]【POI2012】Tour de Byteotia)
- hdu 6092 Rikka with Subset(逆向01背包+思维)
- HEVC代码学习30:fillMvpCand函数
- 卡尔曼滤波的理解以及推导过程
- 线性表应用_约瑟夫环问题
- MT4函数_读取CSV格式文件存入到二维数组
- 5.4银行不同期限的年息利率
- Storm之——搭建Storm集群
- 松柏先生:参透五百年不遇的《求贤令》,何愁品牌不立!
- poj2718 枚举排列