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 ...
随机推荐
- OpenCV 学习笔记(9)RGB转换成灰度图像的一个常用公式Gray = R*0.299 + G*0.587 + B*0.114
https://blog.csdn.net/fly_wt/article/details/86432886 RGB转换成灰度图像的一个常用公式是:Gray = R*0.299 + G*0.587 + ...
- .net core 原生支持grpc
文章转自:https://grpc.io/blog/grpc-on-dotnetcore This is a guest post by Sourabh Shirhatti, a Program Ma ...
- 如何优化SpringBoot的项目的启动速度
日常开发SpringBoot项目启动类都用@SpringBootApplication,实际上它是下面三个注解的组合: @EnableAutoConfiguration: enable Spring ...
- 洛谷 P1825 【[USACO11OPEN]玉米田迷宫Corn Maze】
P1825 传送门 简单的题意 就是一个有传送门的迷宫问题(我一开始以为是只有1个传送门,然后我就凉了). 大体思路 先把传送门先存起来,然后跑一下\(BFS\). 然后,就做完了. 代码鸭 #inc ...
- 使用adb连接Mumu模拟器
1)下载Mumu模拟器 2)运行Mumu模拟器 3)找到mumu安装目录下的MuMu\emulator\nemu\vmonitor\bin目录 4)在当前目录打开cmd,执行 adb connect ...
- GEO Gene Expression Omnibus
GEO Gene Expression Omnibus 基因表达数据库 网址:https://www.ncbi.nlm.nih.gov/geo/ GEO的数据存储方式 GEO数据库具体存放四类数据: ...
- 牛顿迭代法(c++)
编写一个用牛顿法解方程x=tanx 的程序,求最接近4.5和7.7的根 #include <iostream> #include <cmath> using namespace ...
- [Gamma]Scrum Meeting#2
github 本次会议项目由PM召开,时间为5月27日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 前端显示屏幕,翻译 ...
- js Map的使用
setExpenseAndAmountSum: function() { var detailList = vehicleVueObj.vehicleData; var expenseAmountSu ...
- Better intuition for information theory
Better intuition for information theory 2019-12-01 21:21:33 Source: https://www.blackhc.net/blog/201 ...