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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <cstring>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int MAX_N = 100000 + 100;
  10.  
  11. struct Node
  12. {
  13. int val, i;
  14. };
  15. Node arr[MAX_N];
  16.  
  17. int cmp(Node a, Node b)
  18. {
  19. return a.val > b.val;
  20. }
  21. int lowbit(int x)
  22. {
  23. return x & (-x);
  24. }
  25. int s, limit,sum = 0;
  26. int path[MAX_N], num;
  27.  
  28. int main()
  29. {
  30. scanf("%d%d", &s, &limit);
  31. for(int i = limit; i >= 1; i--)
  32. {
  33. arr[i].val = lowbit(i);
  34. arr[i].i = i;
  35. }
  36. sort(arr + 1, arr + 1 + limit, cmp);
  37. int sum = 0;
  38. for(int i = 1; i <= limit; i++)
  39. {
  40. if(arr[i].val + sum > s)
  41. continue;
  42. sum += arr[i].val;
  43. path[num++] = arr[i].i;
  44. if(sum == s)
  45. break;
  46. }
  47. if(sum == s)
  48. {
  49. printf("%d\n", num);
  50. printf("%d", path[0]);
  51. for(int i = 1; i < num; i++)
  52. printf(" %d", path[i]);
  53. puts("");
  54. }
  55. else
  56. puts("-1");
  57. return 0;
  58. }

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. 给表格设置border还可以这样玩

    <table width="100%" border="0" cellpadding="0" cellspacing="1& ...

  2. 《Python 二三事》——python学习必看(转载)

        面向初学者介绍Python相关的一些工具,以及可能遇到的常见问题. 原文出处 原文作者:八八年出生的男性,互联网上常用id是 jagttt .目前正从事 IT 行业的工作.业余爱好是动漫游加电 ...

  3. ajax验证用户名和找回密码参考

    // JavaScript Document function chkname(form){ var user = form.user.value; if(user == ''){ alert('请输 ...

  4. Inter系列处理器名称浅析

    东拼西凑之作,仅仅整理而已,望周知 ------------------------------------------------------------------ 举例 CPU酷睿i5-3230 ...

  5. python中归并排序

    # coding=UTF-8 #!/usr/bin/python import sys def merge(nums, first, middle, last): "merge" ...

  6. IPython学习笔记

    IPython 前言 Life is short, you need Python 最近开始学习数据挖掘,需要使用到Python,其实大学时代就有接触过Python,但是却始终没有系统的进行学习过. ...

  7. 测试通过Word直接发布博文

    这里是来自word 2013的一篇测试文章. 测试直接通过Word自带的bloger功能发布博客文章. 这里插入一张图片

  8. [cocoapods速成] cocoapods的基本用法和自制 podspec

    1 安装方法 主要命令: sudo gem install cocoapods ------------------------------------------------------------ ...

  9. 分布式系统间通信之RPC的基本概念(六)

    RPC(Remote Procedure Call Protocol)远程过程调用协议.一个通俗的描述是:客户端在不知道调用细节的情况下,调用存在于远程计算机上的某个对象,就像调用本地应用程序中的对象 ...

  10. Dungeon Master(poj 2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...