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 ...
随机推荐
- Modular Production Line (MCMF)
Modular Production Line \[ Time Limit: 1000ms\quad Memory Limit: 65536kB \] 题意 给出 \(N\) 种零件,现在你可以用连续 ...
- Vuejs简介
一.网站交互方式 ①传统的开发方式:PHP 中,页面和服务端糅合在一起,在这种项目中服务端占比更重,因为绝大多数都服务端技术,绝大多数网站都是这样的方式 ②前后端分离方式:服务端只处理数据(不关心页面 ...
- RookeyFrame 代码层面 常用方法
测试代码均写在这个类里面的,因为是测试嘛,所以表名那些就将就看了.最后写完了再贴上全部代码 类的路径:Rookey.Frame.Operate.Base -> Test -> Class1 ...
- HDU 4828 小明系列故事——捉迷藏
漂亮妹子点击就送:http://acm.hdu.edu.cn/showproblem.php?pid=4528 Time Limit: 500/200 MS (Java/Others) Memo ...
- 【JZOJ6214】【20190614】tetris
题目 这是一道和俄罗斯方块有关的有趣题目 底面宽度为\(N\),高度无限,初始时方块高度为\(A_i\) 你可以决定每次会下落一个\(1 \times K\)或者\(K \times 1\)的方块 你 ...
- hive基础知识二
1. Hive的分区表 1.1 hive的分区表的概念 在文件系统上建立文件夹,把表的数据放在不同文件夹下面,加快查询速度. 1.2 hive分区表的构建 创建一个分区字段的分区表 hive> ...
- Comet OJ - Contest #15 题解
传送门 \(A\) 咕咕 const int N=1005; int a[N],n,T; int main(){ for(scanf("%d",&T);T;--T){ sc ...
- uni-app 项目记录
await等候,等待:期待 什么是async.awaitawait 用于等待异步完成通常async.await都是跟随Promise一起使用的 async返回的都是一个Promise对象同时async ...
- java并发编程(三)cpu cache & 缓存一致性
一 cpu cache 1. cache的意义 为什么需要CPU cache?因为CPU的频率太快了,快到主存跟不上,这样在处理器时钟周期内,CPU常常需要等待主存,浪费资源.所以cache的出 ...
- ppt使用记录之添加带圈的20以内的数字编号