题目链接

  • 题意:

    给定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. mysql error: Access denied for user 'root'@'localhost' (using password: YES)

    昨天重装了下系统,安装好mysql后,安装了客户端工具连接mysql,提示Access denied for user 'root'@'localhost' (using password: YES) ...

  2. koa 笔记 运行错误

    按照 演示的代码 直接运行会出错,大家需要调整方式. http://koajs.cn/ 要安装以下 $ npm install -g n$ n 0.11.12$ node --harmony my-k ...

  3. Northwind数据库表字段介绍

    ① Categories:种类表 相应字段: CategoryID :类型ID: CategoryName:类型名; Description:类型说明; Picture:产品样本 ② Customer ...

  4. hive权威安装出现的不解错误!(完美解决)两种方法都可以

    以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...

  5. 【多线程】Java并发编程:Lock(转载)

    原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized ...

  6. 【多线程】JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  7. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  8. labview视频采集IMAdx

    grab(连续采集) 摄像头打开之后便一直采集图像,存储在IMAQ开辟的临时空间里,只要while循环不断的读取临时空间就可以显示当前图像(grab调用的是image句柄)

  9. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath(转载)

    1.Request.ApplicationPath->当前应用的目录   Jsp中, ApplicationPath指的是当前的application(应用程序)的目录,ASP.NET中也是这个 ...

  10. Top 7 Myths about HTTPS

    Myth #7 – HTTPS Never Caches People often claim that HTTPS content is never cached by the browser; p ...