Codeforces 437B The Child and Set
题目链接: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的更多相关文章
- codeforces 437B. The Child and Set 解题报告
题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces 437E The Child and Polygon(间隔DP)
题目链接:Codeforces 437E The Child and Polygon 题目大意:给出一个多边形,问说有多少种切割方法.将多边形切割为多个三角形. 解题思路:首先要理解向量叉积的性质,一 ...
- Codeforces 437A The Child and Homework
题目链接:Codeforces 437A The Child and Homework 少看了一个条件,最后被HACK掉到203名,要不然就冲到100多一点了==.. 做这个题收获最大的是英语,A t ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- Codeforces 437D The Child and Zoo(并查集)
Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...
- 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 ...
- 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 ...
- codeforce 437B The Child and Set
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- 给表格设置border还可以这样玩
<table width="100%" border="0" cellpadding="0" cellspacing="1& ...
- 《Python 二三事》——python学习必看(转载)
面向初学者介绍Python相关的一些工具,以及可能遇到的常见问题. 原文出处 原文作者:八八年出生的男性,互联网上常用id是 jagttt .目前正从事 IT 行业的工作.业余爱好是动漫游加电 ...
- ajax验证用户名和找回密码参考
// JavaScript Document function chkname(form){ var user = form.user.value; if(user == ''){ alert('请输 ...
- Inter系列处理器名称浅析
东拼西凑之作,仅仅整理而已,望周知 ------------------------------------------------------------------ 举例 CPU酷睿i5-3230 ...
- python中归并排序
# coding=UTF-8 #!/usr/bin/python import sys def merge(nums, first, middle, last): "merge" ...
- IPython学习笔记
IPython 前言 Life is short, you need Python 最近开始学习数据挖掘,需要使用到Python,其实大学时代就有接触过Python,但是却始终没有系统的进行学习过. ...
- 测试通过Word直接发布博文
这里是来自word 2013的一篇测试文章. 测试直接通过Word自带的bloger功能发布博客文章. 这里插入一张图片
- [cocoapods速成] cocoapods的基本用法和自制 podspec
1 安装方法 主要命令: sudo gem install cocoapods ------------------------------------------------------------ ...
- 分布式系统间通信之RPC的基本概念(六)
RPC(Remote Procedure Call Protocol)远程过程调用协议.一个通俗的描述是:客户端在不知道调用细节的情况下,调用存在于远程计算机上的某个对象,就像调用本地应用程序中的对象 ...
- Dungeon Master(poj 2251)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...