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,括号匹配不需要 ...
随机推荐
- recyclerView 嵌套 recyclerView 点击被拦截
上层recyclerView.setLayoutFrozen(true);下层可以接收到点击事件.
- Swift 构造过程
构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个属性设置初始值和为其执行必要的准备和初始化任务. Swift 构造函数使用 init() 方法. 与 Ob ...
- pip使用笔记
例子: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U funcat -i: 指定库的安装源 -U:升级 原来已经安装的包,不带U ...
- swift 第七课 xib 约束的优先级
前期要实现 一个自适应的label 的时候,就知到xib 约束是有优先级的,一直为深入研究: 乘写这个 博客的机会 ,试验下xib 约束的等级 …… 抱歉要查资料,在重新实践,先把主要浏览的网页 连接 ...
- Allegro静态铜皮避让问题
使用Allegro的人都知道,Allegro的铜分为静态和动态,我的设计习惯是需要满足载流地方一般使用静态铜皮,避免设计过程中因为打孔把铜皮割裂,这是静态铜皮的一个特性,不会自动避让,强制打孔或者走线 ...
- Tomcat 部署web 项目
转载,原文链接: https://www.cnblogs.com/ysocean/p/6893446.html 侵删 回到顶部 3.Tomcat 的目录结构 回到顶部 4.部署项目的第一种方法(项目直 ...
- 【ARM-Linux开发】gstreamer教程及在DM3730上的应用
感谢原文作者:goalie高义http://blog.csdn.net/goalietech/article/details/24887955 1 Gstreamer基本概念 GStreamer 是一 ...
- IDEA使用mybatis generator自动生成代码
主要就三步: 1.pom 文件中引入jar包并配置 build 属性 <dependencies> <!-- 自动生产mapper Begin! --> <depende ...
- 通过JS动态追加标签,以父评论子评论为例
以下代码前后端交互以Django模板语法为例 先来以伪代码来示意用法: HTML部分: JS动态插入部分代码: 运行之后我们来浏览器看检查打印的内容: 看插入前后打印结果我们可以得知 $title[0 ...
- HNOI 2012/codevs 1994:排队
题目描述 Description 某中学有n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个 ...