当前位置:网站首页>Codeforces Round #765 (Div. 2) D. Binary Spiders
Codeforces Round #765 (Div. 2) D. Binary Spiders
2022-06-26 13:58:00 【__ LazyCat__】
Any XOR greater than or equal to k
link :Problem - D - Codeforces
The question : Given n Number , Find the largest set , Make the XOR of any two numbers in the set greater than or equal to k, Find the largest set and output the subscript of the number in the set .
Answer key : First , about k k k highest 1 1 1 Bits above , The XOR of any two higher bits that are different must be greater than k k k Of , So you can put all the numbers in order first , From the highest to k k k Highest bit enumeration , In this case, the numbers with different bit states during enumeration can be divided into two sets , How to choose the number in two sets will not make the number between two sets different or produce less than k k k Number of numbers . But to k k k be equal to 1 1 1 when , The number in the current set can only be selected at most 2 2 2 The number of current binary bits in different states ensures that the current bit XOR is 1 1 1 , But even if XOR is 1 1 1 There is no guarantee that greater than k k k, So we can traverse the maximum XOR number of each number in this interval , If there is more than k k k Then choose these two numbers , If not, just choose one ( Only when the rest of the intervals have access to data, you can choose any one to ensure that the number exclusive or with the rest of the intervals is greater than or equal to k k k). As for how to find the number with the largest XOR value between a number and a number in a certain interval , It can be persistent 01 t r i e 01trie 01trie For maintenance , Of course, you can also build a tree for each searched interval 01 t r i e 01trie 01trie , However, it is not recommended to build trees in search .
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<functional>
#include<queue>
#include<unordered_map>
#include<map>
#include<set>
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll inf=1e18;
const int maxn=3e5+5;
struct trie{
int now=1;
int t[maxn*40][2],val[maxn*40],ct[maxn*40],rt[maxn];
inline void insert(int a,int b,int x,int y)
{
rt[b]=++now;
int l=rt[a],r=rt[b];
for(int i=30;i>=0;i--)
{
int p=x>>i&1;
ct[r]=ct[l]+1;
t[r][p^1]=t[l][p^1];
t[r][p]=++now;
l=t[l][p],r=t[r][p];
}
val[r]=y,ct[r]=ct[l]+1; return;
}
inline int query(int l,int r,int x,int k)
{
l=rt[l],r=rt[r];
for(int i=30;i>=0;i--)
{
int p=x>>i&1;
if(k>>i&1)
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
l=t[l][p^1],r=t[r][p^1];
}
else return 0;
}
else
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
r=t[r][p^1],l=t[l][p^1];
for(int j=i-1;j>=0;j--)
{
if(ct[t[r][p^1]]-ct[t[l][p^1]])
{
r=t[r][p^1],l=t[l][p^1];
}
else
{
r=t[r][p],l=t[l][p];
}
}
break;
}
else r=t[r][p],l=t[l][p];
}
}
return val[r];
}
}t;
void solve()
{
int n,k; cin>>n>>k;
vector<int>ans;
vector<P>a(n+1);
for(int i=1;i<=n;i++)
{
cin>>a[i].first,a[i].second=i;
}
sort(a.begin()+1,a.end());
for(int i=1;i<=n;i++)
{
t.insert(i-1,i,a[i].first,a[i].second);
}
function<void(int,int,int,int,bool)>dfs=[&](int l,int r,int sum,int dep,bool lim)
{
if(l>r)return;
if(k>>dep&1)
{
int flag=0;
for(int i=l;i<=r;i++)
{
int p=t.query(l-1,r,a[i].first,k);
if(p)
{
ans.push_back(a[i].second);
ans.push_back(p);
flag=1; break;
}
}
if(!flag&&lim)ans.push_back(a[l].second);
}
else
{
int p=lower_bound(a.begin()+l,a.begin()+1+r,P(sum+(1<<dep),0))-a.begin();
dfs(l,p-1,sum,dep-1,p<=r||lim);
dfs(p,r,sum+(1<<dep),dep-1,l<p||lim);
}
return;
};
if(k)
{
dfs(1,n,0,30,0);
if(!ans.size()&&a[n].first>=k)
{
ans.push_back(a[n].second);
}
if(ans.size())
{
cout<<ans.size()<<"\n";
for(auto i:ans)cout<<i<<" ";
}
else cout<<-1;
}
else
{
cout<<n<<"\n";
for(int i=1;i<=n;i++)cout<<i<<" ";
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t=1; //cin>>t;
while(t--)solve();
return 0;
}
边栏推荐
猜你喜欢

hands-on-data-analysis 第三单元 模型搭建和评估

Postman自动化接口测试

Cloudcompare - Poisson reconstruction

Free machine learning dataset website (6300+ dataset)

8. Ribbon load balancing service call

【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示

What is the use of index aliases in ES

Stream常用操作以及原理探索

A must for programmers, an artifact utools that can improve your work efficiency n times

Es snapshot based data backup and restore
随机推荐
ICML 2022 | limo: a new method for rapid generation of targeted molecules
Wechat applet SetData dynamic variable value sorting
虫子 STL string 下 练习题
[node.js] MySQL module
泰山OFFICE技术讲座:使用字体粗体的四种情形
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
2021-10-18 character array
D check type is pointer
7-1 range of numbers
Self created notes (unique in the whole network, continuously updated)
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
ES基於Snapshot(快照)的數據備份和還原
微信小程序注册指引
Niuke challenge 48 e speed instant forwarding (tree over tree)
DataGrip配置的连接迁移
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
[scoi2016] lucky numbers
程序员必备,一款让你提高工作效率N倍的神器uTools
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
Mathematical design D12 according to string function