题目链接: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. java dos下中文乱码

    代码如下: public class PrintString{ public static void main(String args[]){ System.out.println("\\* ...

  2. YZOI回忆录&&YZOI3.0介绍&&某些资源的分享

    “那段时光就像块透明的琉璃,美得那么虚幻.飘渺.可是它毕竟在我生命里闪现,哪怕如萤光一样微弱,却照亮了我整个心房.”1.前序伴随着yzoi2.0版本离我而去的是我半年的OI生涯,在这半年内我步入了一个 ...

  3. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  4. photoshop cc 版本安装失败解决办法

    好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: Please see speci ...

  5. mysql数据类型——字符串char(m)和varchar(m)

    char(m) 定长字符串类型  非 Unicode 字符 varchar(m) 变长字符串类型  非 Unicode 数据 说明:M为最大可存储字节数 汉子占两个字节,通过指定m,来限制存储的最大字 ...

  6. run_command函数分析

    一.概述 位置:common/main.c 功能:根据传入参数(命令),在命令存储区(.u_boot_cmd)中查找对应的命令,找到命令并调用对应的函数执行 流程: 二.分析 1.函数说明信息 /** ...

  7. BZOJ 1143 祭祀

    Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成 ...

  8. ubuntu删除openjdk,安装 Sun JDK

    1.到官网下载安装包: jdk-7-linux-i586.tar.gz 2.创建安装目录:sudo mkdir /usr/lib/jvm 3. 解压缩:tar zxvf ./jdk-7-linux-i ...

  9. PHP打印各种金字塔!

    PHP打印各种金字塔! <?php for($i=1;$i<=7;$i++){ for($j=1;$j<=5-$i;$j++){ echo ''; } for($k=1;$k< ...

  10. build Intent

    Intent用于activity之间, fragmetn之间, 或者APP间通信, 主要包含数据和Action两部分: 常见的action是字符串形式的activity指定 Intent intent ...