codeforces --- Round #250 (Div. 2) B. The Child and Set
【题目大意】
给你一个sum和一个limit,现在要你在1~limit中找到一些数来使得这些数的和等于sum,如果能找到的话就输出找到的数的个数和这些数,未找到输出"-1"。
比赛的时候被hack了。
【题目分析】
这题需要将所有的数的lowbit先求出来,然后按照大小排序,然后从后往前判断,如果这个数小于sum那么这个数就是可以构成sum的数,选进去,完了以后判断sum的值是否为0就可以了。做题的时候没将题目理解透彻。
#include<bits/stdc++.h>
#define MAX 100010
using namespace std;
struct Node
{
int num;
int id;
bool vis=;
};
Node node[MAX];
int sum,limit;
int lowbit(int x)
{
return x&(-x);
}
bool cmp(Node a,Node b)
{
return a.num<b.num;
}
int main()
{
scanf("%d%d",&sum,&limit);
int i,j;
for(i=;i<=limit;i++)
{
node[i].num=lowbit(i);
node[i].id=i;
}
sort(node+,node++limit,cmp);
int cnt=;
for(i=limit;i>=;i--)
{
if(node[i].num<=sum)
{
sum-=node[i].num;
node[i].vis=;
cnt++;
}
} if(sum)
printf("-1");
else
{
printf("%d\n",cnt);
for(i=limit;i>=;i--)
{
if(node[i].vis)
{
printf("%d ",node[i].id);
}
}
}
putchar();
return ;
}
codeforces --- Round #250 (Div. 2) B. The Child and Set的更多相关文章
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...
- Codeforces Round #250 (Div. 1) A. The Child and Toy 水题
A. The Child and Toy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- Codeforces Round #250 (Div. 2)—A. The Child and Homework
好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人 被HACK 1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #250 (Div. 2)B. The Child and Set 暴力
B. The Child and Set At the children's day, the child came to Picks's house, and messed his house ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- MongoDB远程连接-命令行客户端mongo.exe
命令行客户端mongo.exe 位于安装目录bin子目录下.MongoDB的所有可执行程序都在其中. 双击打开mongo.exe应该是默认连接本地数据库服务,因此需要用Cmd或Powershell的方 ...
- Openwrt build env setup(9)
reference : https://openwrt.org/docs/guide-developer/quickstart-build-images Install dependence pack ...
- linux命令之------touch命令
touch命令 1) 作用:用于修改文件或目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 2) -a:改变档案的读取时间记录: 3) -m:改变档案的 ...
- Javascript总结(全)
说明 个人感觉总结得很好忍不住要分享,转载自github地址,作者博客原文发现打不开,于是复制粘贴到此处,如有侵权,请联系本人删除. Type class TypeFn { isString (o) ...
- 重置GPU显存 Reset GPU memory after CUDA errors
Sometimes CUDA program crashed during execution, before memory was flushed. As a result, device memo ...
- Excel填坑[0]
Excel填坑[0] 本着一天水一贴的原则(放p),我又来填坑了.今天做一个很简单的排队图,虽然不难,但因为手机显示问题折腾了半天.感觉做图做表格不仅仅是靠技术,更重要的是思维. 就是这张图,看起来平 ...
- Intellij idea利用Statistic插件统计项目代码行数
1 . 插件介绍统计项目中各个文件的数量,大小,行数,平均等信息根据扩展名自定义统计详细行数信息 , 包括总行数,代码行数,代码行数占比,注释行数,注释行数占比,空白行数,空白行数占比自定义选择多个文 ...
- linux下如何使用adb连接在qemu中运行的安卓系统?
1. 运行安卓系统, 如下: $sudo qemu-system-x86_64 -m 4096 -boot d -enable-kvm -smp 3 -net nic -net user,hostfw ...
- css---【vw,vh】进行自适应布局单位
在进行CSS3自适应布局,会用到 vw 和 vh 进行布局 视口单位(Viewport units) 什么是视口? 在桌面端,视口指的是在桌面端,指的是浏览器的可视区域:而在移动端,它涉及3个视口:L ...
- angular的路由例子
app.routing.module.ts里面,关键部分 const routes: Routes = [ { path: '', redirectTo : 'c3/c2/mmc', pathMatc ...