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 ...
随机推荐
- php 安装zip模块
为php安装zip扩展 wget http://pecl.php.net/get/zip-1.13.5.tgz tar -zvxf zip-1.13.5.tgz cd zip-1.13.5 /home ...
- 查找服务器的真实ip
最近做项目遇到很多网站使用了cdn,导致扫目录等操作很难进行. 于是学习了一下绕过cdn的一下方法,记录一下. 大致分为下面几种方法: 1.查找子域名 实际上这个方法不一定有用因为很多子域名并不是和W ...
- vue-cli脚手架快速搭建项目
前言 vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. 准备工作 在搭建一个vue项目之前,需要先安装好node.js和cnpm. 虽然np ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- bzoj 3626
http://www.lydsy.com/JudgeOnline/problem.php?id=3626 让我比较惊讶的一道链剖裸题(' ' ) 做法很精妙 首先我们考虑对于单个询问时可以拆分成 ...
- horizontalAccuracy 检测定位成功
- (void)findCurrentLocation { self.isFirstUpdate = YES; [self.locationManager startUpdatingLocation] ...
- 当前系统的CPU和内存的空闲百分比
设想我们有一个php页面A比较耗资源,因此在每次执行页面A中的代码前需要检测一下系统目前CPU和内存的空闲百分比.我们可以利用下面几个函数来解决这个问题 1 2 3 4 5 6 7 8 9 10 11 ...
- 71、salesforce的JSON方法
List<Merchandise__c> merchandise = [select Id,Name,Price__c,Quantity__c from Merchandise__c li ...
- 64位系统sql链接oracle
在SQL Server 2008中连接Oracle,完成查询.插入操作 建立指向Oracle的连接 在32位的系统中sql链接oracle,在链接服务器里点击服务器对象,右键链接服务器,选择micro ...
- 引用so动态链接库的方法
from ctypes import cdll lib = cdll.LoadLibrary('/home/zhengli/test/test.so') lib.func() 总结: 1.引用ctyp ...