题目链接

  • 题意:

    给定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. 推荐一个网站Stack Overflow

    网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...

  2. 再来说说Activity

    经过前面多天的了解,现在可以确信一点: activity提供了用户和程序交互的界面. 而且android里有四大组件:Activity,Service,BroadcastReceiver,Conten ...

  3. scala: How to write a simple HTTP GET request client in Scala (with a timeout)

    Scala CookBook: http://scalacookbook.com/ @throws(classOf[java.io.IOException]) @throws(classOf[java ...

  4. AudioManager --- generateAudioSessionId

    AudioManager中的generateAudioSessionId方法介绍: 1.方法声明 pubilc void generateAudioSessionId(); 2.API描述 返回一个不 ...

  5. Linux_搜文件

    Linux 下搜文件, 通常先用 whereis 或 locate ,如果找不到,才以 find 搜寻!因为 whereis 与 locate 是利用数据库来搜寻数据,省时间! <<鸟哥的 ...

  6. MySQL Connector_J_5.1.34_2014.10

    5.1版本符合JDBC3.0和JDBC4.0规范 跟MySQL4.1-5.7兼容 5.1.21以后支持JDK7的JDBC4.1规范 在MySQL4.1之前,是不支持utf8的 com.mysql.jd ...

  7. quartz 的job中获取到applicationContext

    第一步: 定义SchedulerFactoryBean的applicationContextSchedulerContextKey <bean name="scheduler" ...

  8. ADT-bundle

    eclipse 弹出 Version 1.4.2_03 of the JVM not suitable for this product.Version1.6or geeater is requir ...

  9. 让MySQL支持中文

    这两天在学习webpy,把webpy的一个blog例子扒下来学习一下,默认创建的table当存入中文的时候是乱码,研究了一下这个问题. 1,创建table的时候就使用utf8编码 举个例子: crea ...

  10. Domino 迁移到Exchange 服务器 之在Domino Server 创建用户!

    我们打开Lotus Admin,导航到注册,点击到需要设置的人,然后再选择验证者标识符选择相应的组织配置标识符: