Codeforces F. Bits And Pieces(位运算)
位运算的比较基本的题。
考虑枚举\(i\),然后二进制位从大到小考虑, 对于第\(w\)位,如果\(a[i][w]=1\),那么对\(j、k\)并没有什么限制。
如果\(a[i][w]=0\),那么我们希望\((a[j]~and~a[k])[w]=1\),结合前面的限制,就是给定\(x\),问有没有\(x∈a[j]~and~a[k](i<j<k)\)。
那么这应该是做一个高维后缀max和次max就能解决的事。
Code:
#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define ff(i, x, y) for(int i = x; i < y; i ++)
#define fd(i, x, y) for(int i = x; i >= y; i --)
#define pp printf
#define ll long long
using namespace std;
const int N = 1e6 + 10;
int n, a[N];
const int M = 1 << 21;
int f[M], g[M];
void add(int x, int i) {
if(x > f[i]) {
g[i] = f[i];
f[i] = x;
} else
if(x > g[i]){
g[i] = x;
}
}
const int inf = 1e9;
int main() {
scanf("%d", &n);
fo(i, 1, n) {
scanf("%d", &a[i]);
add(i, a[i]);
}
fo(i, 0, 20) {
ff(j, 0, 1 << 21) {
if(!(j >> i & 1)) {
add(f[j + (1 << i)], j);
add(g[j + (1 << i)], j);
}
}
}
ff(j, 0, 1 << 21) {
if(f[j] != g[j]) {
f[j] = g[j];
} else {
f[j] = 0;
}
}
int ans = 0;
fo(i, 1, n - 2) {
int x = 0;
fd(j, 20, 0) {
if(!(a[i] >> j & 1)) {
x += 1 << j;
if(g[x] <= i) {
x -= 1 << j;
}
}
}
ans = max(ans, x | a[i]);
}
pp("%d", ans);
}
Codeforces F. Bits And Pieces(位运算)的更多相关文章
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp
F. Bits And Pieces 题面 You are given an array
- F. Anton and School 位运算 + 化简
http://codeforces.com/contest/734/problem/F 因为 x + y = (x & y) + (x | y) 有了这个公式后,然后应该手动模拟一下,把公式化 ...
- Codeforces 1208F - Bits And Pieces(高维前缀和)
题面传送门 题意:求 \(\max\limits_{i<j<k}a_i|(a_j\&a_k)\). \(1\leq n \leq 10^6,1\leq a_i\leq 2\time ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- CF1208F Bits And Pieces
CF1208F Bits And Pieces 传送门 思路 这里要运用SOS-DP的思路(\(\text{Sum over Subsets}\)).我在另外一篇博客里介绍过,如有需要可以搜索一下我的 ...
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- Codeforces Round #443 (Div. 2) C 位运算
C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- 【leetcode】688. Knight Probability in Chessboard
题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...
- 微信小程序 摇一摇
本次案例是有一点像微信的那个摇一摇但是没有那么好看,一些APP也有看到摇一摇功能. 小程序虽然没有提供摇一摇API接口,但是也一个加速器API ,加上搜索一些大神的资料,我这里就做了一个dome, ...
- 初学Linux基本的命令操作应当记牢
Linux管理文件和目录的命令 命令 功能 命令 功能 pwd 显示当前目录 ls 查看目录下的内容 cd 改变所在目录 cat 显示文件的内容 grep 在文件中查找某字符 cp 复制文件 touc ...
- mysql5.6配置-my
# mkdir -p /home/mysql/3306/{data,binlog,logs} [client] port = socket=/tmp/my3306.sock [mysql] no-au ...
- Github上的SlidingMenu菜单的使用方法
GitHub上的SlidingMenu的配置方法 1. 首先明确SlidingMenu是什么? 2. 如何使用SlidingMenu框架呢? GitHub上的SlidingMenu的配置方法 1. 首 ...
- PHP的安装配置
一.安装 PHP的安装可以很简单的使用yum命令进行安装. #添加php7.0源(这是centos7的命令,centos6.5的命令不同,不要照搬)rpm -Uvh https://dl.fedora ...
- Rust <7>:数据结构==>链表
enum List { Cons(u64, Box<List>), NULL, } impl List { fn new() -> List { List::NULL } fn pr ...
- vue中nextTick的使用
最近使用vue的vm.$nextTick的用法比较多,现在整理一下它的用法. 推荐阅读:http://www.ruanyifeng.com/blog/2014/10/event-loop.html ...
- Emacs25.1之后UrlHttpError
Emacs25.1之后UrlHttpError */--> pre.src {background-color: #002b36; color: #839496;} pre.src {backg ...
- Encode
by kinsly 本文的内容均基于python3.5 编码一直是python中的大坑,反正我是一直没搞明白,今天在做爬虫的时候,觉得实在是有必要把这些东西整理一下. 什么是编码 简单的来说就是,为了 ...