D. Binary Spiders(dp&Trie)

题意

给定nnn个点的点权和参数kkk

求最大完全子图,满足任意边权≤k\le k≤k,边权等于两点的点权异或值。

思路

前置知识:对于nnn个数的最小异或和对就等于相邻异或和的最小值。

所以排序后就可以dpdpdp转移了。

这样是O(n2)O(n^2)O(n2)的。

考虑优化,我们可以用TrieTrieTrie。

存当前子树的最大dpdpdp下标。

每次就遍历一遍TrieTrieTrie 即可。

输出答案的话就用一个数组记录。

时间复杂度:O(nlogn)O(nlogn)O(nlogn)

#include<cstdio>
#include<algorithm>
#define M 10000010
#define N 3000010
using namespace std;
int cnt=1,f[N],lst[N],tr[M][2],t[M];
void getmax(int&a,int b)
{if(f[a]<f[b]) a=b;
}
const int B=30;
void insert(int x,int id)
{int now=1;for(int i=B;i>=0;i--){bool y=(x&(1<<i));if(!tr[now][y]) tr[now][y]=++cnt;now=tr[now][y];getmax(t[now],id);}
}
int n,k;
struct node{int x,y;
}a[N];
bool cmp(node a,node b){return a.x<b.x;}
int query(int x)
{int ans=0,now=1;for(int i=B;i>=0;i--){bool y=(x&(1<<i)),z=(k&(1<<i));if(z){if(!tr[now][y^1]) return ans;now=tr[now][y^1];}else{getmax(ans,t[tr[now][y^1]]);now=tr[now][y];}}getmax(ans,t[now]);return ans;
}
int main()
{scanf("%d%d",&n,&k);for(int i=1;i<=n;i++)scanf("%d",&a[i].x),a[i].y=i;sort(a+1,a+n+1,cmp);for(int i=1;i<=n;i++)lst[i]=query(a[i].x),f[i]=f[lst[i]]+1,insert(a[i].x,i);int w=0;for(int i=1;i<=n;i++) getmax(w,i);if(f[w]==1){printf("-1");return 0;}printf("%d\n",f[w]);for(int i=w;i;i=lst[i]) printf("%d ",a[i].y);
}

D. Binary Spiders(dpTrie)相关推荐

  1. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  2. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  3. Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView

    今天创建了一个 demo ,然后就是复制RecyclerView 过去, 到最后完成运行的时候发现 系统奔溃了, 然后 提示 Binary XML file line #8: Error inflat ...

  4. Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin

    Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin 这个问题自己大致在 ...

  5. android.view.InflateException: Binary XML file line #7: Binary XML file line #7

    错误如下 11-21 08:19:44.040 3608-3608/com.leon.oldrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main   ...

  6. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  8. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  9. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

  10. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

最新文章

  1. 使用文件操作函数实现:文件的复制功能。
  2. 跟无闻学习GO Web 编程(四) -- bee 工具使用
  3. 你在第几楼?80后、90后扎心图鉴
  4. 大概看了一下《Flash MX 2004 -- 数据库应用开发 - 基于.NET架构》,感觉有点迷惘了!...
  5. 移除链表元素--虚拟头结点
  6. Android注册会员系统返回上一步时如何保存信息
  7. 通过Java反射来理解泛型的本质
  8. 加入收藏 设为首页 代码:兼容IE和FireFox
  9. python机器学习系列教程——深度学习框架比较TensorFlow、Theano、Caffe、SciKit-learn、Keras
  10. MyBatis mapper parameterType
  11. JAVA获取word书签内容_Java 操作Word书签(一):添加、删除、读取书签
  12. 机器学习实战-Python机器学习项目实战
  13. .NET如何对接PayPal支付
  14. 几组数据的相关性python_几的定义
  15. HDU 1517 A Multiplication Game 巴什博弈
  16. 分享 java 基础 + 进阶精简资料(视频 + 源码 + 就业项目 + 面试报装)
  17. PHP+Laravel+Fpdi+Fpdf 在PDF模板上编辑自定义文字并输出新PDF文件示例
  18. Qt入门(一)——自己动动手写一个简易的用户化界面(Qt命令行模式)
  19. 英语阅读小程序云开发项目
  20. LeetCode 每日一题1584. 连接所有点的最小费用

热门文章

  1. 插入参考文献时,本该显示为[1],但显示了{Zhao,2017,#253},解决方法
  2. react中使用谷歌地图并定位
  3. 解决VMware Workstation Pro15 无法在Windows上运行
  4. c++ ‘-DNODE_GYP_MODULE_NAME=libsass‘ ‘-DUSING_UV_SHARED=1‘ ‘-DUSING_V8_SHARED=1‘ ‘-DV8_DEPRECATION_
  5. 站在22岁的边上--我的2007
  6. NR SRB and message transfer
  7. mysql character_set_client 设置_关于MySQL如何修改character_set_client的编码问题
  8. html发短信模版,短信模板管理.html
  9. Test-time Adaptation论文合集
  10. 新浪微博 redis mysql_新浪微博:史上最大的Redis集群