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内部只能有一个子元素 ...
随机推荐
- SpringMVC 拦截器(interceptors)对样式(css),JavaScript(js),图片(images)链接的拦截
因为在web.xml配置了 <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pa ...
- Color 颜色码-英文名称-十六进制-RGB对照表
色 英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 # ...
- solr 3.5 配置及server设置
一.solr 的简单介绍 Apache Solr 是一个开源的搜索server.Solr 使用 Java 语言开发,主要基于 HTTP 和 Apache Lucene 实现.Apache Solr 中 ...
- javascriipt类型转换
- VS2013服务器资源管理器添加Mysql数据源
如何为VS2013服务器资源管理器添加Mysql数据源,如图: 接下来就看下如何添加 1.需要下载安装Mysql for Visual Studio 1.1.1 下载位置:http://downloa ...
- OD: Shellcode Encoding
Shellcode 受到的限制 1. 大多数情况下 shellcode 中不允许出现 0x00 截断符,这个可以通过特殊指令来做到. 2. 有时候 shellcode 必须为可见的 ASCII 字符或 ...
- spring-security 登陆认证之初次探究
首先,希望还对 spring-security框架完全不懂的新手 下载下Git源码. 引入到项目中.这个短文就是边看源码边聊的.也会启动下项目验证自己的推想. 一.登陆认证的登陆配置项 <for ...
- cocos2dx 帧动画(iOS)
植物大战僵尸的植物摇摆效果 //帧动画 Animation *animation = Animation::create(); Sprite *sprite = Sprite::create(&quo ...
- 服务器端操作Cookie[2]
服务器端操作Cookie,主要注意会使用以下三个类: HttpCookie,HttpResponse,HttpRequest 关于HttpCookie: 属性 描述 例子 Domain 获取或设置与此 ...
- WdatePicker 设置今天起 后30天可选
<link href="{:ADDON_PUBLIC_PATH}/style/My97DatePicker/skin/WdatePicker.css" rel="s ...