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,括号匹配不需要 ...
随机推荐
- UML期末复习题——2.9:UML Deployment Diagram
附加题:部署图 重要概念: 1. 部署图 部署图表示的是,如何将具体的软件制品(例如可执行文件)分配到计算节点(具有处理服务的某种事物)上.部署图表示了软件元素在物理架构上的部署,以及物理元素之间的通 ...
- Windows Bat 之For 循环
Windows Bat 之For 循环 1. For 循环基本用法. 1.1 格式 在cmd窗口中: FOR %variable IN (set) DO command [command-pa ...
- JDK的新特性
JDK5新特性 a,自动拆装箱 b,泛型 c,可变参数 d,静态导入 e,增强for循环 f,互斥锁 g,枚举 JDK7新特性 * A:二进制字面量 * B:数字字面量可以出现下划线 * C:swit ...
- c++ throw异常(学习)
#include <iostream>#include <stdio.h> using namespace std; void my_copy(const char* src_ ...
- 通过举例了解java中的流
Java流结构介绍: Java所有的流类位于java.io包中,都分别继承字以下四种抽象流类型. 字节流 字符流 输入流 InputStream Reader 输出流 OutputStream W ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- Vue学习笔记(三)组件间如何通信传递参数
一:父组件向子组件传递参数 <template > <div id="app"> <h1 v-text="title">&l ...
- spring注入注解
常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Component. @Resource装配顺序 1. 如果同时指定了n ...
- Ubuntu 16.04 RabbitMq 安装与运行
前言目前公司用阿里云 + redis 的方式实现的消息队列.了解了目前几种主流的消息组件(主要包括rabbitmq.kafka.)的优缺点后,这里为了深入学习rabbitmq,我在自己的腾讯云服务器上 ...
- 知识点整理-网络IO知识总结
UNIX 系统下的 I/O 模型有 5 种 同步阻塞 I/O.同步非阻塞 I/O.I/O 多路复用.信号驱动 I/O 和异步 I/O 什么是I/O 所谓的I/O 就是计算机内存与外部设备之间拷贝数据的 ...