The Child and Set
来源:互联网 发布:休闲单机游戏 知乎 编辑:程序博客网 时间:2024/05/16 12:35
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82662#problem/C
题目大意:
小孩子玩儿坏了我的set,我要把它重新按方法整理好:
1、set的范围在limit以内
2、set中每个元素的值等于其二进制的第一个1所对应的大小: lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation).
找到元素之和等于sum的set S
位运算的题,以前一直不会位运算,虽然现在会的也不多
运算方法:lowbit(a) = a & (-a);
对数字,按lowbit之后的值从大到小进行排序,然后,小于等于sum的就加入ans数组,同时sum的值减小,如此;如果没有满足和等于sum的set存在,输出1
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;#define N 100005struct node{ int value; int num;};bool cmp(node a, node b){ return a.value > b.value;}int main(){ int sum, limit; while(cin >> sum >> limit) { node s[N]; for(int i = 1; i <= limit; i++) { s[i].num = i; s[i].value = i&(-i); }// for(int i = 1; i <= limit; i++)// cout << s[i].num << " " << s[i].value << endl;// cout <<endl; sort(s+1,s+1+limit,cmp); int ans = 0; int out[N]; int k = 0; if(s[1].value <= sum) { out[k++] = s[1].num; ans += s[1].num; } for(int i = 2; i <= limit; i++) if(ans+s[i].value<=sum) { ans += s[i].value; //cout << " " << s[i].num; out[k++] = s[i].num; if(ans == sum) break; } if(sum != ans) cout << "-1" << endl; else { cout << k << endl; cout << out[0]; for(int i = 1; i < k; i++) cout << " " << out[i]; cout << endl; } } return 0;}
0 0
- The Child and Set
- The Child and Set
- cf437B The Child and Set
- CodeForces The Child and Set
- Codeforces 437B The Child and Set
- codeforce 437B The Child and Set
- CodeForces 437B The Child and Set
- 250 B. The Child and Set
- Codeforces div.2 B. The Child and Set
- Codeforces #250 (Div. 2) B. The Child and Set
- codeforces 06/01/B The Child and Set
- Codeforces 437B The Child and Set(暴力)
- Codeforces Round #250 (Div. 2) -B. The Child and Set
- 【线段树】Codeforces 437B The Child and Set
- G - The Child and Set-CodeForces 437B-水题
- CF437 B. The Child and Set(贪心)
- The Child and Toy
- Codeforces Round #250 (Div. 2)——The Child and Set
- hash冲突的解决方法
- Java 中Comparable和Comparator区别比较
- hdu 1969 Pie(贪心+二分查找)(简单)
- Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换
- ACM技巧总结
- The Child and Set
- 如何获得select标签下option的值
- 【Unity探究】物理碰撞实验
- Android学习之SQLite学习
- 2012年5月SAT香港真题解析
- jdbc-dao
- 修改webview的字体大小比例,背景颜色,以及字体颜色
- Clojure经典实例
- Android音乐播放器 -- 数据处理