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内部只能有一个子元素 ...
随机推荐
- 2016年如何选择 Linux 发行版
不管是在企业级应用还是在消费者领域,2015 对于 Linux 来说都是极其重要的一年.作为一个从 2005 年就开始使用 Linux 的老用户,我有幸见证了 Linux 过去这 10 年里的重大发展 ...
- angular 数据加载动画 longding
由于公司服务器架构不行,每次加载数据都要很久,但是都是使用angular来渲染数据,不像jquery有beforsend什么的方法, 这是一个github上找的,很轻使用也很简单 1.安装 npm ...
- Could not Build module ImageIO
错误提示:Could not Build module ImageIO: 原因:修改了XCode API 源文件 措施:从一个干净的未曾修改过的XCode中拷贝一份相应的文件添加到发生错误的XCode ...
- Appium依据xpath获取控件实例随笔
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- LSI SAS 3008配置操作
配置 LSI SAS 3008 介绍LSISAS3008的配置操作. 4.1 登录CU界面 介绍登录LSISAS3008的CU配置界面的方法. 4.2 创建RAID 介绍在LSISAS3008扣卡上创 ...
- Sqlserver2012数据库乱码的解决方法
Sqlserver2012数据库乱码的解决方法 1. 在创建数据库时,一定要指定数据库的排序规则 2. 输入数据库名称 3. 选中选项,在排序规则中选中Chinese_P ...
- 关于JS历史拓展
js由来 95年那时,绝大多数因特网用户都使用速度仅为28.8kbit/s 的“猫”(调制解调器)上网,但网页的大小和复杂性却不断增加.为完成简单的表单验证而频繁地与服务器交换数据只 ...
- css实现垂直居中6种方法
在一次次笔试,一次次的面试中,问到垂直居中的问题太多太多,但是我每一次回答,都好像都不能让面试官太满意,今天特意花点时间,整理一下css垂直居中问题. 1.如果是单行文本.看代码: <!DOCT ...
- 调用有道翻译API
前两天朋友说起NASA开放了数据API,今儿突然想起从来没用过外部提供的API,然而简单用得多的貌似是有道词典API,就像试试,本来觉得应该挺简单的,用urllib模块很快就实现了. 不过测试时才发现 ...
- 打开局域网项目,显示“项目位置不受信任”的解决办法(VS2008)
弄了几天,网上搜了个遍,愣是解决不了,绝望的时候闭着眼睛胡搞,居然解决了,哈哈.... 开发环境:visual studio 2008 项目位置:局域网其他电脑内 出现问题: 1.弹出“”的对话框,如 ...