[CF1849F] XOR Partition
XOR Partition
题目描述
For a set of integers $ S $ , let's define its cost as the minimum value of $ x \oplus y $ among all pairs of different integers from the set (here, $ \oplus $ denotes bitwise XOR). If there are less than two elements in the set, its cost is equal to $ 2^{30} $ .
You are given a set of integers $ {a_1, a_2, \dots, a_n} $ . You have to partition it into two sets $ S_1 $ and $ S_2 $ in such a way that every element of the given set belongs to exactly one of these two sets. The value of the partition is the minimum among the costs of $ S_1 $ and $ S_2 $ .
Find the partition with the maximum possible value.
输入格式
The first line contains $ n $ ( $ 2 \le n \le 200000 $ ) — the number of elements in the set.
The second line contains $ n $ distinct integers $ a_1 $ , $ a_2 $ , ..., $ a_n $ ( $ 0 \le a_i < 2^{30} $ ) — the elements of the set.
妙妙题。
二分出来 \(m\),然后去看 \(a_i\oplus a_j<m\) 的所有 \(i,j\) 是不是组成二分图。明显要黑白染色。
如何知道一个数列中最小的 \(a_x\oplus a_y(x\ne y)\)? 有两种方法,而这两种方法衍生出这题的两种做法。
1,字典树
这个东西可以用字典树求。
考虑用字典树优化暴力建图。在跑字典树的途中,向小于 \(m\) 的所有子树连边,会连 \(\log n\) 次。
但是我不能连向自己所在的节点。所以要前后缀加上可持久化字典树就可以了。复杂度 \(O(nlog^2n)\),这是我考场上想到的方法,但是没写。
这题还有另一个单 log 的字典树做法。但没看懂
2
将 \(a\) 从小到大排序后 \(\min\limits_{i=1}^{n-1} a_i\oplus a_{i+1}\) 就是答案。因为异或存在性质:如果 \(x<y<z\),则 \(\min(x\oplus y,y\oplus z)<x\oplus z\)
这里也一样,将 \(a\) 从小到大排序后,如果 \(a_i\oplus a_{i+j}<m(j\ge 4)\),那么一定无解。考虑 $a_{i}\oplus a_{i+j} $ 的最高位,中间夹的这五个数可能是 \(\{0,0,0,0,1\},\{0,0,0,1,1\},\{0,0,1,1,1\},\{0,1,1,1,1\}\),然后这五种都存在三元环。
考虑一个 \(a_i\),我们只让他和 \(a_{i+1},a_{i+2},a_{i+3}\) 去连边。但是这样好像还是会有一个问题,如何证明这样连边合法的情况下,不存在连了后面的边后才会出现非法情况。但是这样做是能过的。希望有大佬可以给个证明或 hack。OI比赛中还是打 Trie 计算除了前三个是否存在 \(a_i\oplus a_j<m\) 或者打拍比较保险。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N],col[N],fl,e_num,hd[N],l=1,r=(1<<30)-1,id[N],p[N];
struct edge{
int v,nxt;
}e[N<<3];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9')
s=s*10+ch-48,ch=getchar();
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s;
}
void dfs(int x)
{
for(int i=hd[x];i;i=e[i].nxt)
{
if(!~col[e[i].v])
col[e[i].v]=col[x]^1,dfs(e[i].v);
else if(col[e[i].v]^1^col[x])
fl=1;
}
}
int ok(int x)
{
memset(hd,e_num=fl=0,sizeof(hd));
memset(col,-1,sizeof(col));
for(int i=1;i<=n;i++)
for(int j=1;j<=3&&j+i<=n;j++)
if((a[i]^a[i+j])<x)
add_edge(i,i+j),add_edge(i+j,i);
for(int i=1;i<=n;i++)
if(!~col[i])
col[i]=0,dfs(i);
return fl^1;
}
int cmp(int x,int y)
{
return a[x]<a[y];
}
int main()
{
n=read();
if(n==2)
return puts("01"),0;
for(int i=1;i<=n;i++)
a[i]=read(),id[i]=i;
sort(id+1,id+n+1,cmp);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
p[id[i]]=i;
while(l<=r)
{
int md=l+r>>1;
if(ok(md))
l=md+1;
else
r=md-1;
}
ok(r);
for(int i=1;i<=n;i++)
putchar(col[p[i]]+48);
}
[CF1849F] XOR Partition的更多相关文章
- ARC 066D Xor Sum AtCoder - 2272 (打表找规律)
Problem Statement You are given a positive integer N. Find the number of the pairs of integers u and ...
- Partition:增加分区
在关系型 DB中,分区表经常使用DateKey(int 数据类型)作为Partition Column,每个月的数据填充到同一个Partition中,由于在Fore-End呈现的报表大多数是基于Mon ...
- Partition:Partiton Scheme是否指定Next Used?
在SQL Server中,为Partition Scheme多次指定Next Used,不会出错,最后一次指定的FileGroup是Partition Scheme的Next Used,建议,在执行P ...
- Partition:分区切换(Switch)
在SQL Server中,对超级大表做数据归档,使用select和delete命令是十分耗费CPU时间和Disk空间的,SQL Server必须记录相应数量的事务日志,而使用switch操作归档分区表 ...
- sql 分组取最新的数据sqlserver巧用row_number和partition by分组取top数据
SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系 ...
- Oracle Partition Outer Join 稠化报表
partition outer join实现将稀疏数据转为稠密数据,举例: with t as (select deptno, job, sum(sal) sum_sal from emp group ...
- SQLServer中Partition By 函数的使用
今天群里看到一个问题,在这里概述下:查询出不同分类下的最新记录.一看这不是很简单的么,要分类那就用Group By;要最新记录就用Order By呗.然后在自己的表中试着做出来: 首先呢我把表中的数据 ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- 每日一库:gosec
gosec 是一个用于在 Go 代码中查找安全问题的开源工具,它可以帮助发现可能的漏洞和潜在的安全风险.以下是关于 gosec 的详细介绍: 1. 工具概述: gosec 是一个静态分析工具,用于扫描 ...
- Java 多线程处理 for 循环数据
对于必须在for循环内进行查询的场景,可采用以下几种方式进行优化. 1.主线程与子线程无先后顺序 public static void main(String[] args) throws Inter ...
- Job System 初探
作者:i_dovelemon 日期:2023-08-24 主题:Fiber, Atomic Operation, MPMC Queue, Multiple thread, Job system 引言 ...
- vue3探索——组件通信之依赖注入
背景 通常情况下,当我们需要从父组件向子组件传递数据时,会使用 props.想象一下这样的结构:有一些多层级嵌套的组件,形成了一颗巨大的组件树,而某个深层的子组件需要一个较远的祖先组件中的部分数据.在 ...
- 京东获得JD商品详情 API 返回值说明
item_get-获得JD商品详情 API测试 onebound.jd.item_get 公共参数 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中) ...
- git status详解
git status详解 在使用Git进行版本控制时,git status是一个非常有用的命令,用于查看当前代码仓库的状态.它可以告诉你哪些文件已更改.添加或删除,以及是否有未提交的更改等.本篇博客文 ...
- LSTM推导
LSTM推导 forward propagation def lstm_cell_forward(xt, a_prev, c_prev, parameters): """ ...
- 【krpano】KRPano自动缩略图分组插件
该插件可以展示场景缩略图,并支持场景分组. 下载地址:http://pan.baidu.com/s/1dFj7v0l 使用说明: 插件共有两个文件,auto_thumbs.xml和tooltip.xm ...
- 使用 Sealos 一键部署高可用 MinIO,开启对象存储之旅
大家好!今天这篇文章主要向大家介绍如何通过 Sealos 一键部署高可用 MinIO 集群. MinIO 对象存储是什么? 对象是二进制数据,例如图像.音频文件.电子表格甚至二进制可执行代码.对象的大 ...
- 解决 wg-quick 在 Mac 上 bash 3 无法运行的问题
问题原因 我可以理解,开发人员不想使用苹果使用的旧bash v3.但从用户的帖子来看,安装一个较新的bash并不那么好 所以我看了wireguard的wg-quick.需要支持的唯一变化,两个bash ...