CF336C-Vasily the Bear and Sequence题解--贪心
题目链接
https://www.luogu.org/problemnew/show/CF336C
分析
一个比较妙的贪心
我们要让最后\(and\)起来的数被\(2^k\)整除且\(k\)最大,我们不妨从后往前枚举\(k\),同时运用贪心的思路,对于二进制第\(k\)为1的数,我们想让最后得到的数除第\(k\)位外都为0,当然是\(and\)越多越好
代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <queue>
#include <bitset>
#define ll long long
#define ri register int
using std::min;
using std::max;
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;return ;
}
const int maxn=100005;
const int inf=0x7fffffff;
int a[maxn],n,q[maxn];
int main(){
int x,y,z;
int ans=0;
read(n);
for(ri i=1;i<=n;i++){
read(a[i]);
}
for(ri k=30;k>=0;k--){
x=(1<<k);
y=x-1;
ans=0;
for(ri i=1;i<=n;i++){
//printf("%d %d\n",a[i],x);
if(a[i]&x){
y=y&a[i];
q[++ans]=a[i];
}
}
if(y==0){
printf("%d\n",ans);
for(ri i=1;i<=ans;i++){
printf("%d ",q[i]);
}
puts("");
return 0;
}
}
return 0;
}
CF336C-Vasily the Bear and Sequence题解--贪心的更多相关文章
- codeforces 336C Vasily the Bear and Sequence(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Sequence Vasily the b ...
- C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)
C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...
- codeforces C. Vasily the Bear and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...
- CF336A Vasily the Bear and Triangle 题解
Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...
- codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Beautiful Strings Vas ...
- 【CF486E】LIS of Sequence题解
[CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 ...
- 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)
科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...
- hdu 6299 Balanced Sequence(贪心)题解
题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...
随机推荐
- zookeeper系列 (第三章 :zookeeper 的使用)
接上一章,在启动客户端之后,开始通过命令操作zookeeper 服务. 一:zookeeper 的基础命令 1.通过zkCli.sh 命令与主机建立一个会话 2.开始在会话中执行命令:写入Znode. ...
- LeetCode —— 单词接龙(Python)
使用字典,降低查找的复杂度.使用list会超时. class Solution: def nextWordsList(self, word, wordDict): res_list = [] for ...
- Netty解码器相关文章
最通用TCP黏包解决方案:LengthFieldBasedFrameDecoder和LengthFieldPrepender https://blog.csdn.net/u010853261/arti ...
- Java同步数据结构之CopyOnWriteArrayList/CopyOnWriteArraySet
前言 前面介绍完了队列(包括双端队列),今天探讨以下Java并发包中一个List的并发数据结构实现CopyOnWriteArrayList,顾名思义CopyOnWriteArrayList也是一种基于 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_13-页面静态化-数据模型-轮播图DataUrl接口
要开发轮播图的DataUrl的接口 轮播图的配置的集合 xc-framework-model这个module下 CmsConfigModel的类的属性 定义接口 在api里面定义接口:CmsConfi ...
- protobuf / Consul / 边缘计算 / MEC / CDN / Serverless / GraphQL / 微服务 / 网关 / 云原生 / Serverless (真能造概念啊!!!)
技术概念层出不穷,学吧!记录下自己多这些概念的理解 protobuf: 数据结构而已,类比XML, JSON consul 解决的只是微服务里的服务注册与发现,健康检查等. 边缘计算:可以理解为是指利 ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- 生命周期和Zend引擎
一切的开始:SAPI接口 SAPI(Server Application Programming Interface)指的是具体应用的编程接口.PHP脚本要执行有很多种方式,通过web服务器,或者直接 ...
- linux 文件赋权限
chown tomcat:tomcat /logs chmod 766 /logs
- DRF-路由
路由Routers 对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供 ...