[note]一类位运算求最值问题
[note]一类位运算求最值问题
给定一些数,让你从中选出两个数a,b,每次询问下列中的一个
1.a and b的最大值
2.a xor b的最大值
3.a or b的最大值
神仙们都是FWT,小蒟蒻只好orz
首先三种问题的思路都是从高位往低位贪心
- 对于xor,直接枚举每个数Trie树上贪心
- 对于and,如果可选集合中有大于等于两个数当前位为1,那么答案这一位也是1,并把这一位为0的数删去
- 对于or,先维护一个高维前缀和,枚举每一个数,如果某一位为0,我们就贪心的看能不能补上1,
相当于询问是否存在某个数满足前面的要求并且这一位为1,
如果这一位是1,显然我们在这位放0限制作用更小.
复杂度都是\(nlog\)值域
不过预处理高位前缀和似乎是\(log\)值域×值域
#include<bits/stdc++.h>
using namespace std;
const int _=1e5+5;
int re(){
int x=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
}
int T,n,c,ans,tot;
int a[_],ch[2][_*20];
bool ban[_],vis[1<<21];
void solve_and(){
memset(ban,0,sizeof(ban));
for(int i=20;~i;i--){
int cnt=0;
for(int j=1;j<=n;j++)
if(!ban[j]&&a[j]>>i&1)cnt++;
if(cnt<2)continue;ans|=1<<i;
for(int j=1;j<=n;j++)
if(a[j]>>i&1^1)ban[j]=1;
}
}
void insert(int x){
int u=0;
for(int i=20;~i;i--){
int k=x>>i&1;
if(!ch[k][u])ch[k][u]=++tot;
u=ch[k][u];
}
}
int query(int x){
int res=0,u=0;
for(int i=20;~i;i--){
int k=x>>i&1^1;
if(!ch[k][u])u=ch[k^1][u];
else u=ch[k][u],res|=1<<i;
}
return res;
}
void solve_xor(){
tot=0;
memset(ch,0,sizeof(ch));
for(int i=1;i<=n;i++)insert(a[i]);
for(int i=1;i<=n;i++)ans=max(ans,query(a[i]));
}
void solve_or(){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)vis[a[i]]=1;
for(int i=1<<20;~i;i--)
for(int j=0;j<=20;j++)
if(vis[i]&&i>>j&1)vis[i^(1<<j)]=1;
for(int i=1;i<=n;i++){
int res=0,sum=0;
for(int j=20;~j;j--){
sum|=1<<j;
if(a[i]>>j&1)continue;
if(vis[res|(1<<j)])res|=1<<j;
else sum^=1<<j;
}
ans=max(ans,sum);
}
}
int main(){
freopen("maximum.in","r",stdin);
freopen("maximum.out","w",stdout);
T=re();
while(T--){
n=re(),c=re();ans=0;
for(int i=1;i<=n;i++)a[i]=re();
if(c==1)solve_and();
if(c==2)solve_xor();
if(c==3)solve_or();
printf("%d\n",ans);
}
return 0;
}
[note]一类位运算求最值问题的更多相关文章
- 位运算求最值 学习笔记 (待补充QAQ)
没有什么前言?直接进入正题qwq 俩俩异或 求最值: 建trie树 O(n)枚举每个数找这个数的最值,每次反走就成,还可以剪枝一波(如果在某位已经小于ans显然可以直接return? void Ins ...
- C语言学习笔记之位运算求余
我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注意:该方法只对除数是2的N次方幂时才有效. ...
- c语言小技巧:C语言学习笔记之位运算求余
我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不 允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注 意:该方法只对除数是2的N次方幂时才有效 ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3) via Hackbuteer1
转自:http://blog.csdn.net/hackbuteer1/article/details/7390093 ^: 按位异或:&:按位与: | :按位或 计算机系统中,数值一律用补码 ...
- hdu 4336 Card Collector (概率dp+位运算 求期望)
题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- java位运算求一个整数的绝对值
1 import java.util.Scanner; 2 3 public class Question1 { 4 public static void main(String[] args) { ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- C++中巧妙的位运算
位运算要多想到与预算和异或运算,并常常将两个数对应位上相同和不同分开处理 一.x&(x-1)消除x二进制中最右边的一个1. 这个比较厉害,比如统计某个 二.与和异或的巧妙结合的思想 与运算可以 ...
随机推荐
- HTML5 Canvas 用requestAnimation取代setInterval
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- 又见The request sent by the client was syntactically incorrect ()
前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好 ...
- java web 分页实现
分页实现的效果: ///////// /////////////////////////////////////////////////////// /////////////////// ...
- Unable to connect to a repository at URL 解决方法
提示"Unable to connect to a repository at URL 'svn://localhost/project1/'" or “Can't connect ...
- oc 把view添加到rootcontrollerview控制的view
在当前活跃的window 添加一个view [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] add ...
- 【cocos2dx开发技巧10】cocosStudio的集成以及c++11的新特性
转发.请保持地址:http://blog.csdn.net/stalendp/article/details/38880997 非常长时间没有碰cocos2dx了,近期又用起来了.花了好几个小时又一次 ...
- Webpack与Gulp、Grunt区别
Webpack与Gulp.Grunt没有什么可比性,它可以看作模块打包机,通过分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等 ...
- Http调试工具-Fiddler使用指引
转自:http://my.oschina.net/u/1388024/blog/186886#OSC_h1_9 目录[-] Fiddler是什么? Fiddler能做什么? 从哪里下载? 安装: 初次 ...
- two sum, three sum和four sum问题
1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash ...
- 4Sum_leetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...