题目链接:Codeforces 437B The Child and Set

開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200。100,60。50。S = 210。那先选了200肯定就不行了,所以写了个DFS。果断TLE。后来想了想lowbit之后的值都是最高位是1。其余位都是0的二进制数,所以上边的情况是不会出现的,至于原因我感觉我说不清楚。。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 100000 + 100; struct Node
{
int val, i;
};
Node arr[MAX_N]; int cmp(Node a, Node b)
{
return a.val > b.val;
}
int lowbit(int x)
{
return x & (-x);
}
int s, limit,sum = 0;
int path[MAX_N], num; int main()
{
scanf("%d%d", &s, &limit);
for(int i = limit; i >= 1; i--)
{
arr[i].val = lowbit(i);
arr[i].i = i;
}
sort(arr + 1, arr + 1 + limit, cmp);
int sum = 0;
for(int i = 1; i <= limit; i++)
{
if(arr[i].val + sum > s)
continue;
sum += arr[i].val;
path[num++] = arr[i].i;
if(sum == s)
break;
}
if(sum == s)
{
printf("%d\n", num);
printf("%d", path[0]);
for(int i = 1; i < num; i++)
printf(" %d", path[i]);
puts("");
}
else
puts("-1");
return 0;
}

Codeforces 437B The Child and Set的更多相关文章

  1. codeforces 437B. The Child and Set 解题报告

    题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...

  2. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  3. Codeforces 437E The Child and Polygon(间隔DP)

    题目链接:Codeforces 437E The Child and Polygon 题目大意:给出一个多边形,问说有多少种切割方法.将多边形切割为多个三角形. 解题思路:首先要理解向量叉积的性质,一 ...

  4. Codeforces 437A The Child and Homework

    题目链接:Codeforces 437A The Child and Homework 少看了一个条件,最后被HACK掉到203名,要不然就冲到100多一点了==.. 做这个题收获最大的是英语,A t ...

  5. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

  6. Codeforces 437D The Child and Zoo(并查集)

    Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...

  7. Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树

    Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...

  8. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  9. codeforce 437B The Child and Set

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. 《vi中的替换艺术》-linux命令五分钟系列之十一

    vi方面的内容不知道分类到哪里好,就放到<Linux命令五分钟系列>里吧! 今天编程,关于栈的一个小例子,其间我需要把”S.”替换为”S->”(替换不包括双引号). 其实这个不难,不 ...

  2. 修改原代码定制bootstrap

    1.下载对应的Bootstarap和node.js 注:less文件夹中包含了bootstrap中所有样式组件的less源代码: dist保存编译后的css和js等文件 2.命令行输入npm inst ...

  3. 数组去重的三种方法及from方法

    直接上代码: var str="adbbckddwerivka"; var arr=str.split(""); console.log(arr); //ind ...

  4. 利用sfntly的sfnttool.jar提取中文字体

    雨忆博客中提到了sfntly(具体介绍可以看:https://code.google.com/p/sfntly/),利用其中sfnttool.jar就可以提取只包含指定字符的字体,如果想在页面中通过@ ...

  5. 【技术宅3】截取文件和url扩展名的N种方法

    //截取文件扩展名的N种方法   //第1种 //strrchr() 函数查找字符在指定字符串中最后一次出现的位置,如果成功,则返回其后面的字符串 //返回带有点的扩展名 function get_e ...

  6. yii 验证确认密码是否一致 【"compare",'compareAttribute'=>'password'】

    array("surepassword","compare",'compareAttribute'=>'password','message'=>' ...

  7. TCP协议握手与分手

    TCP(Transmission Control Protocol) 传输控制协议 TCP的7次握手可以理解为3次握手和4次分手. TCP状态转换图,如下: 这个图N多人都知道,它对排除和定位网络或系 ...

  8. Tag Helpers 介绍

    Tag Helpers 介绍 原文:Introduction to Tag Helpers作者:Rick Anderson翻译:刘浩杨校对:高嵩(Jack) 什么是 Tag Helpers? Tag ...

  9. NET Core 静态文件及JS包管理器(npm, Bower)的使用

    NET Core 静态文件及JS包管理器(npm, Bower)的使用 文章目录 在 ASP.NET Core 中添加静态文件 使用npm管理JavaScript包 使用Bower管理JavaScri ...

  10. 总结iOS 8和Xcode 6的各种坑

    模拟器的路径从之前的~/Library/Application Support/iPhone Simulator移动到了~/Library/Developer/CoreSimulator/Device ...