题目链接

  • 题意:

    给定goal和limit,求1-limit中的若干个数,每一个数最多出现一次,且这些数的lowbit()值之和等于goal,假设存在这种一些数,输出个数和每一个数;否则-1
  • 分析:

    先考虑一下比較普通的情况,给一些数,和一个goal,问时候能达到。(最好还是设这些数已经从大到小排序)

    考虑能否够贪心,对于当前的数x:

    1、之后的数的和能等于x,那么假设x<=goal,显然必须选x;

    2、之后的数的和能等于x-1,那么同上(这个情况就是二进制的情况)

    3、之后的数的和不包含上述两个情况,那么不能贪心(推測)



    再分析这个题,是符合第一个情况的。对于第i + 1位,当第i位出现两个1时候,之后才会在i + 1位出现一个1,所以符合第一个情况,能够贪心

lowbit()函数事实上就是一个数的二进制最低位的1代表的十进制数值

const int MAXN = 25;

int lowbit(int n)
{
return n & -n;
} int bin(int n)
{
int ret = 0;
while (n)
{
n >>= 1;
ret++;
}
return ret - 1;
}
vector<int> G[MAXN]; int main()
{
// freopen("in.txt", "r", stdin);
int sum, limit;
while (~RII(sum, limit))
{
REP(i, MAXN) G[i].clear();
vector<int> ans;
int s = 0;
FE(i, 1, limit)
{
int t = bin(lowbit(i));
G[t].push_back(i);
s += lowbit(i);
}
if (s < sum)
puts("-1");
else
{
FED(i, 22, 0)
{
int val = (1 << i);
int ct = min((int)G[i].size(), sum / val);
REP(j, ct)
ans.push_back(G[i][j]);
sum -= ct * val;
}
WI(ans.size());
REP(i, ans.size())
cout << ans[i] << ' ';
puts("");
}
}
return 0;
}

Codeforces Round #250 (Div. 2)——The Child and Set的更多相关文章

  1. 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 ...

  2. 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/ ...

  3. 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/ ...

  4. 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, ...

  5. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. “内部类” 大总结(Java)

    (本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...

  2. seo技巧-2015/10/05

    1.每页都要有它自己的文件名,并且有它自己的上级文件夹和它自己相关关键字. 2.建议在每页上使用一个的H1标签.我也试着使用许多H2 或H3的标签在页面内辅助构成正文内容. 3. 有时花费一点钱帮助你 ...

  3. C语言练习:第二大整数

    问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来.说明:(1)0表示输入结束,它本身并不计入这组整数 ...

  4. web前端笔试题

    1, 判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/; reg.test ...

  5. 客户端无法tcp连接上本地虚拟机的问题(最后是linux防火墙问题)

    刚装好裸的centos6.5,很多东西跟以前比都是没有的,所以做起来会遇到很多问题. 今天刚把svn 无法ci的问题解决了,起完服后,发现客户端连不上. 1)端口转发,查看了一下虚拟机的端口转发,发现 ...

  6. [cocos2d-js]chipmunk例子(二)

    ; ; ; ; <<; var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT; var MainLayer = cc.Layer.extend({ _bal ...

  7. js 发红包

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. dom 拖拽div

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 【原】创建Hive表,分号分隔符“;”引起的异常

    [障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...

  10. Ubuntu 14.04 LTS中怎样解决系统设置残缺的问题

    iBus卸载之后,系统设置会缺失,是Ubuntu 14.04和iBus之间的关系引起的,但是如果直接安装 sudo apt-get install ubuntu-desktop 这个会把amazon广 ...