题目链接

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题解--贪心的更多相关文章

  1. codeforces 336C Vasily the Bear and Sequence(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Sequence Vasily the b ...

  2. 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 ...

  3. codeforces C. Vasily the Bear and Sequence 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...

  4. CF336A Vasily the Bear and Triangle 题解

    Content 一个矩形的顶点为 \((0,0)\),其对顶点为 \((x,y)\),现过 \((x,y)\) 作直线,分别交 \(x\) 轴和 \(y\) 轴于 \(A,B\) 两点,使得 \(\t ...

  5. codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  6. 【CF486E】LIS of Sequence题解

    [CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 ...

  7. 洛谷P2460 [SDOI2007]科比的比赛(题解)(贪心+搜索)

    科比的比赛(题解)(贪心+搜索) 标签:算法--贪心 阅读体验:https://zybuluo.com/Junlier/note/1301158 贪心+搜索 洛谷题目:P2460 [SDOI2007] ...

  8. Least Cost Bracket Sequence(贪心)

    Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...

  9. hdu 6299 Balanced Sequence(贪心)题解

    题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...

随机推荐

  1. Qt数据库编程_基本

    QtSql模块提供了一个平台无关且数据库无关的访问SQL数据库的接口. Qt中的每个数据库连接用一个QSqlDatabase对象来表示:Qt使用不同driver来和各种不同数据库的API进行通讯. Q ...

  2. SOCKET原理(转载)

    SOCKET原理 一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息: ...

  3. WebView调用js方法获取返回值的完美解决方案

    在Android项目中我们或多或少会涉及到与js交互的问题,这其中WebView是必须掌握的控件,今天主要说说我们通过WebView调用js方法,然后如何很好的获取返回值.这里我总结了三种方式,大家可 ...

  4. 文件上传使用FileUpload组件进行代码实现

    使用FileUpload组件进行代码实现 实现步骤 1. 获取解析器工厂: DiskFileItemFactory 2. 获取解析器对象: ServletFileUpload 3. 解析request ...

  5. amm与tmpfs和swap

    /dev/shm对Oracle 11g的影响: ORACLE 从11g版本开始,引入了一个自动内存管理(Automatic Memory Management)特性,该特性需要更多的共享内存(/dev ...

  6. c++ new bad_alloc

    try { for (int i = 0; i<1000; i++) { test1 = new Test(); cout << i << " new dog ...

  7. MLN 讨论 —— inference

    We consider two types of inference: finding the most likely state of the world consistent with some ...

  8. Path环境变量的作用

    作用: 当我们要求系统运行一个程序(例如a.exe)而没有告诉它程序所在的完整路径时,系统会先在当前目录寻找是否存在a.exe,如果找到,直接运行:如果没有找到,会去path路径下面找.设置path, ...

  9. OpenGL(6)——坐标系

    在掌握基本变换后,学习如何变换coordinate space. 对coordinate space进行变换的目的是将local space中各顶点坐标转换成normalized device coo ...

  10. python列表插入--append(), extend(), insert()

    append(),extend(), insert()都是列表操作中常用的插入函数.其中前两个均接收一个参数,并插入到列表尾部.最后一个接收两个参数,将参数2插入到参数1之前. 本文主要讨论appen ...