cf437B The Child and Set
1 second
256 megabytes
standard input
standard output
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S:
- its elements were distinct integers from 1 to limit;
- the value of was equal to sum; here lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105).
In the first line print an integer n (1 ≤ n ≤ 105), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
5 5
2
4 5
4 3
3
2 3 1
5 1
-1
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4.
贪心……从第一位向上贪心,然后每次往上合并。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int bh[20][100010],fa[20][100010],gs[20];
int pre[100010];
int n,limt,sum,ans[100010];
int getw(int x)
{
int res=0;
while(x)
{
x=x>>1;res++;
}
return res-1;
}
void solve()
{
int i,j;
for(i=0;i<=18;i++)
{
if((1<<i) & sum)
{
if(gs[i]>0)
{
for(j=bh[i][gs[i]];j;j=pre[j]) ans[++ans[0]]=j;
gs[i]--;
}
else {printf("-1\n");return;}
}
for(j=1;j<=gs[i];j+=2)
{
if(j+1<=gs[i])
{
pre[fa[i][j+1]]=bh[i][j];
gs[i+1]++;bh[i+1][gs[i+1]]=bh[i][j+1];fa[i+1][gs[i+1]]=fa[i][j];
}
}
}
printf("%d\n",ans[0]);
for(i=1;i<=ans[0];i++) printf("%d ",ans[i]);
}
int main()
{
int i,x,y;
cin>>sum>>limt;
for(i=1;i<=limt;i++)
{
x=(i & (-i));
y=getw(x);
gs[y]++;
fa[y][gs[y]]=i;bh[y][gs[y]]=i;
}
solve();
return 0;
}
cf437B The Child and Set的更多相关文章
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...
- php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault
环境 [root envirotar]# uname -a Linux i2..el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Lin ...
- [ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action
概括的讲,View中的内容可以分为静态和动态两部分.静态内容一般是html元素,而动态内容指的是在应用程序运行的时候动态创建的内容.给View添加动态内容的方式可归纳为下面几种: Inline cod ...
- 调用Child Package
使用Execute Package Task,能够在一个package中调用并执行其他package,被调用的Package称作 Child Package,Execute Package Task ...
- ORA-02292: integrity constraint (xxxx) violated - child record found
在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外 ...
- Child <- many-to-one ->Parent
网上找到个描述的很精妙的例子 Child <- many-to-one ->Parent class Child { private ...
- [NHibernate]Parent/Child
系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...
- ScrollView can host only one direct child
Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...
随机推荐
- Java Hibernate 之连接池详解
Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP.在配置连接池时需要注意的有三点: 一.Apche的DBCP在Hibernate2中受支持,但在Hiber ...
- Spring-mvc junit单元测试中 如何回滚?
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration(value = "src/main/webapp") @C ...
- Zabbix中文使用手册
一.Zabbix简介 详情参考“企业监控利器-zabbix”http://waringid.blog.51cto.com/65148/904201. 二.Zabbix使用 2.1 Zabbix框架介绍 ...
- JS浏览器对象-History对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- (原)Mac下Apache添加限制IP线程模块:mod_limitipconn.so
1.下载:http://dominia.org/djao/limit/mod_limitipconn-0.24.tar.bz2 解压到一个目录:我的是~/Download/mod_limitipcon ...
- css的小demo
demo1 一个高度随宽度变化的正方形 (缩小屏幕试试) 原理:margin和padding如果是用百分比设置,则是以父元素的宽度的百分比设置的. .Square{ display: inline ...
- NSUserDefaults读取和写入自定义对象
NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...
- snappydb 依赖的jar包
最近看学习了一下snappydb,因为我用的还是Eclipse但是github上的是as项目所以就考虑用jar包来使用. github地址:https://github.com/nhachicha/S ...
- Mac下如何不借助第三方工具实现NTFS分区的可写挂载
问题背景 我想很多使用Mac的同学都会遇到读写NTFS磁盘的问题,因为默认情况下Mac OSX对NTFS磁盘的挂载方式是只读(read-only)的,因此把一个NTFS格式的磁盘插入到Mac上,是只能 ...
- Asp.net - The type or namespace name 'App_Code' does not exist in the namespace 'xxx' (are you missing an assembly reference?)
我在 项目 下面创建一个 App_Code的文件夹,然后在其下创建自定义的类,但是当我在该项目下别的地方使用时报错: The type or namespace name 'App_Code' doe ...