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
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?
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).
It is guaranteed that there exists at least one solution. And if there are different solutions, print the lexicographic minimum one.
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]$
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[],b[],a[];
int main()
{
scanf("%d",&t); while(t--)
{
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));//a[i]表示第i个数字的个数
memset(bb,,sizeof(bb));//bb[k]表示当1~k-1中数字个数确定后,凑到和为k的种数,不够就表示,需要单独k这个数字来凑 for(int i=;i<=m;i++)
scanf("%d",&b[i]); k=; bb[]=;
while(k<=m)
{
a[k]=b[k]-bb[k]; //这就是因为1~k-1个数确定后,能凑到和为k的种数,不够的说明a序列中有b[k]-bb[k]个数的k for(int j=;j<=a[k];j++)
{
for(int i=m;i>=k;i--) //反着来,避免已经加到结果里的数字再加一遍,这里有01背包的感觉
bb[i]+=bb[i-k];
}
k++;
} int tot=; //输出
for(int i=;i<=m;i++)
for(int j=;j<=a[i];j++)
{
if (tot++) printf(" ");
printf("%d",i);
}
printf("\n");
}
return ;
}
hdu 6092 Rikka with Subset(逆向01背包+思维)的更多相关文章
- hdu 6092 Rikka with Subset(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092 #include <cstdio> #include <iostream> ...
- HDU 6092`Rikka with Subset 01背包变形
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 6092 Rikka with Subset 01背包 思维
dp[i][j]表示前i个元素,子集和为j的个数.d[i][j] = d[i][j] + d[i-1][j-k] (第i个元素的值为k).这里可以优化成一维数组 比如序列为 1 2 3,每一步的dp值 ...
- HDU 6092 Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 6092 Rikka with Subset (集合计数,01背包)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- HDU 6092 Rikka with Subset(dp)
http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...
- 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- HDU 6092:Rikka with Subset(dp)
分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.3
Maven 导入项目时报错: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.3 ...
- NUMA架构的优缺点
numa把一台计算机分成多个节点(node),每个节点内部拥有多个CPU,节点内部使用共有的内存控制器,节点之间是通过互联模块进行连接和信息交互.因此节点的所有内存对于本节点所有的CPU都是等同的,对 ...
- Let Her Go
转自:https://www.zhihu.com/question/29255072/answer/43962611 [MV]Passenger-Let Her Go-高清MV在线播放 版本一 Let ...
- [Android] 录音与播放录音实现
http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...
- 【Python】装饰器 & 偏函数
[装饰器] 1.最简单的Decorator. def author(f): def addName(): print('My name is xkfx.\n') f() return addName ...
- Python Date 1–Hello world print
对比学习Python与C str1 = 'hello python 2'# 字符串i = 3.1415926535 print(str1)print("hello python\n" ...
- Python3.x:pytesseract识别率提高(样本训练)
Python3.x:pytesseract识别率提高(样本训练) 1,下载并安装3.05版本的tesseract 地址:https://sourceforge.net/projects/tessera ...
- JavScript 日期格式化
JavScript 日期格式化 //日期格式化 function formatDate(date,fmt) { if(date == null || typeof (date) == undefine ...
- JAVA面试题整理(3)-Spring
1.BeanFactory 和 FactoryBean? 2.Spring IOC 的理解,其初始化过程? 3.BeanFactory 和 ApplicationContext? 4.Spring B ...
- 20145310《Java程序设计》第5次实验报告
20145310<Java程序设计>第5次实验报告 实验要求 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验内容 根据所学内容,编写代码实现服务器与客户端 掌 ...