题目链接

  • 题意:

    给定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. Cracking the Code Interview 4.3 Array to Binary Tree

    Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...

  2. 详解centos下vi的用法

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  3. ajax 新闻栏目

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

  4. 【MySql】在Linux下安装MySql数据库

    [参数环境] 1.Host OS:Win7 64bit 2.VM: VMware 11.1.0 3.Client OS:CentOS 6 4.系统中已安装的openssl版本: openssl-1.0 ...

  5. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. PCB中层的定义(一)

  7. Bluetooth in Android 4.2 and 4.3(一):综述

    从Android 4.2开始,Bluetooth stack发生了重大改变:从Bluez换成了由Google和Broadcom联合开发的Bluedroid(当然,核心的部分还是Broadcom的,Go ...

  8. Max retries exceeded with url

    78 Traceback (most recent call last):   File "thread072413.py", line 163, in <module> ...

  9. JSP文件下载时文件名在ie和firefox下面文件名不一致极其超链接中文乱码的问题的改进

    response.setContentType("application/octet-stream;charset=UTF-8"); fileName=java.net.URLEn ...

  10. CentOS6 下MySQL option file

    my.cnf内容如下 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/e ...