CF1446C Xor Tree 题解 DP Trie树

Xor Tree

传送门

题面翻译

给定你一个非负整数序列 a a a,保证其中每个数两两不同。
对于每个 a i a _ i ai,它会向 j ≠ i j \ne i j=i a i ⊕ a j a_i\oplus a_j aiaj ⊕ \oplus 代表异或)最小的 a j a _ j aj 连双向边。
如果 a j a _ j aj 也向 a i a _ i ai 连了边,只算一条边。现在要让你删去序列中的一些数,使得最后形成的图是一颗树,输出最少需要删除几个数。

Translated by 试试事实上吗.

题目描述

For a given sequence of distinct non-negative integers ( b 1 , b 2 , … , b k ) (b_1, b_2, \dots, b_k) (b1,b2,,bk) we determine if it is good in the following way:

  • Consider a graph on k k k nodes, with numbers from b 1 b_1 b1 to b k b_k bk written on them.
  • For every i i i from 1 1 1 to k k k : find such j j j ( 1 ≤ j ≤ k 1 \le j \le k 1jk , j ≠ i j\neq i j=i ), for which ( b i ⊕ b j ) (b_i \oplus b_j) (bibj) is the smallest among all such j j j , where ⊕ \oplus denotes the operation of bitwise XOR (https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Next, draw an undirected edge between vertices with numbers b i b_i bi and b j b_j bj in this graph.
  • We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn’t have any simple cycles).

It is possible that for some numbers b i b_i bi and b j b_j bj , you will try to add the edge between them twice. Nevertheless, you will add this edge only once.

You can find an example below (the picture corresponding to the first test case).

Sequence ( 0 , 1 , 5 , 2 , 6 ) (0, 1, 5, 2, 6) (0,1,5,2,6) is not good as we cannot reach 1 1 1 from 5 5 5 .

However, sequence ( 0 , 1 , 5 , 2 ) (0, 1, 5, 2) (0,1,5,2) is good.

You are given a sequence ( a 1 , a 2 , … , a n ) (a_1, a_2, \dots, a_n) (a1,a2,,an) of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?

It can be shown that for any sequence, we can remove some number of elements, leaving at least 2 2 2 , so that the remaining sequence is good.

输入格式

The first line contains a single integer n n n ( 2 ≤ n ≤ 200 , 000 2 \le n \le 200,000 2n200,000 ) — length of the sequence.

The second line contains $ n $ distinct non-negative integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0ai109 ) — the elements of the sequence.

输出格式

You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.

样例 #1

样例输入 #1

5
0 1 5 2 6

样例输出 #1

1

样例 #2

样例输入 #2

7
6 9 8 7 3 5 2

样例输出 #2

2

提示

Note that numbers which you remove don’t impact the procedure of telling whether the resulting sequence is good.

It is possible that for some numbers b i b_i bi and b j b_j bj , you will try to add the edge between them twice. Nevertheless, you will add this edge only once.

以上来自洛谷 以上来自洛谷 以上来自洛谷

解题思路

可将问题转换为转化为求最大保留数,令只存在一对 ( i , j ) (i,j) (i,j) 对彼此而言 a i ⊕ a j a_i\oplus a_j aiaj 的值都是最小的。

自然想到 Trie 树。(不会 Trie 树?看这里。)把所有数插入进 Trie 树里,递归考虑 Trie 树的一棵子树 x x x,记令子树 x x x 合法的最大保留数为 f x f_x fx,子树 x x x 的左右儿子对应的是二进制意义下的第 k k k 位。

当子树 x x x 只存在左或右儿子时, f x f_x fx 的值就是左右儿子的值,否则就是 max ⁡ ( L e f t S o n . f , R i g h t S o n . f ) + 1 \max(LeftSon.f,RightSon.f)+1 max(LeftSon.f,RightSon.f)+1。因为在第 k k k 位之前,对应位的值都相同,仅第 k k k 位不相同,保留左右子树中的一棵,再保留其余子树的一个节点,就能确保当前子树内只存在一对上述的 ( i , j ) (i,j) (i,j)。最后答案就是 n − f r t n−f_{rt} nfrt

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Maxn = 2e5 + 5;
int n, a[Maxn];
int rt, cnt;
int tong[4000005][2];
inline void Inst(int val) {int tmp = rt, cur;for (int i = 30; i >= 0; i--) {cur = val >> i & 1;if (!tong[tmp][cur]) {cnt += 1;tong[tmp][cur] = cnt;}tmp = tong[tmp][cur];}
}
inline int Fac(int tmp) {if (!tong[tmp][0] && !tong[tmp][1]) {return 1;}if (!tong[tmp][0]) {return Fac(tong[tmp][1]);}if (!tong[tmp][1]) {return Fac(tong[tmp][0]);}return max(Fac(tong[tmp][0]), Fac(tong[tmp][1])) + 1;
}
inline void work() {rt = cnt = 1;cin >> n;for (int i = 1; i <= n; ++i) {cin >> a[i];Inst(a[i]);}cout << n - Fac(rt) << endl;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/623762.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

React18-树形菜单-递归

文章目录 案例分析技巧通信展示效果实现代码技巧点技巧点 Refer to 案例分析 https://github.com/dL-hx/manager-fe/commit/85faf3b1ae9a925513583feb02b9a1c87fb462f7 从接口获取城市数据,渲染出一个树形菜单 要求: 可以展开和收起 技巧 学会递归渲染出一个树形菜单, 并点击后…

力扣-三数之和

三数之和 给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k &#xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请你返回所有和为 0 且不重复的三元组。 注意&#xff1a;答案中不可以包含重复的三元组。…

16. 从零用Rust编写正反向代理, 反向代理upstream源码实现

wmproxy wmproxy是由Rust编写&#xff0c;已实现http/https代理&#xff0c;socks5代理&#xff0c; 反向代理&#xff0c;静态文件服务器&#xff0c;内网穿透&#xff0c;配置热更新等&#xff0c; 后续将实现websocket代理等&#xff0c;同时会将实现过程分享出来&#xff…

gramine运行nodejs例程

首先&#xff0c; 修改js例程代码&#xff1a; const { Web3 } require(web3); const rpcURL "https://sepolia.infura.io/v3/40b89bc0f5584056b19626b521ee5874"; const web3 new Web3(rpcURL); const address "0xde51E698b4585Af1C8322cc084ABbdbDcfe533…

C++力扣题目450--删除二叉搜索树中的节点

给定一个二叉搜索树的根节点 root 和一个值 key&#xff0c;删除二叉搜索树中的 key 对应的节点&#xff0c;并保证二叉搜索树的性质不变。返回二叉搜索树&#xff08;有可能被更新&#xff09;的根节点的引用。 一般来说&#xff0c;删除节点可分为两个步骤&#xff1a; 首先…

Generalized Focal Loss论文个人理解

论文地址&#xff1a;Generalized Focal Loss: Towards Efficient Representation Learning for Dense Object Detection 论文解决问题 研究人员最近更加关注边界框的表示(representation)及其定位质量估计&#xff08;LQE&#xff0c;本论文中指的是IoU score&#xff09;&a…

将YOLO数据集转成COCO格式,单个文件夹转为单个json文件,例如.../images/train转为instance_train.json

写在前面 参考链接&#xff1a;objectdetection-tricks/tricks_4.py 相关视频教学&#xff1a;tricks_4 用于yolov5和v7中的yolo格式转换coco格式的脚本.(如何在v5和v7中输出ap_small,ap_middle,ap_large coco指标)还可以参考相关的VOC转COCO的方式&#xff1a;damo-yolo/voc2…

R语言【paleobioDB】——pbdb_reference():通过参数请求获得多条参考文献的基本信息

Package paleobioDB version 0.7.0 paleobioDB 包在2020年已经停止更新&#xff0c;该包依赖PBDB v1 API。 可以选择在Index of /src/contrib/Archive/paleobioDB (r-project.org)下载安装包后&#xff0c;执行本地安装。 Usage pbdb_references (...) Arguments 参数【...】…

MtimeMtimecmp

Mtime: 实时time计数器&#xff0c;可读可写&#xff1b;mtime必须按照一个固定的频率递增&#xff1b;如果count overflow了&#xff0c;则mtime的值需要卷绕&#xff1b;对于32/64的系统来说&#xff0c;mtime的值都是64bits的&#xff1b; 与mtime对应的&#xff0c;还有一…

【架构师成长之领域驱动开发】

架构师成长之路 1. 如何构建高质量应用&#xff1f;2. 三大设计原则&#xff1f;3.DDD妙招4. 最终的改造结果5.模型 项目中的“坏”味道 可维护性差&#xff1a;大量的第三方模块影响核心代码的稳定性可扩展性差&#xff1a;业务逻辑与数据存储相互依赖&#xff0c;无法复用可测…

项目中使用iframe引入html 解决路由错乱问题以及父子组件传值调用方法

iframe与外部之间传值 父组件 <iframeid"iframe"src"luckysheet/index.html"frameborder"0"scrolling"no"style"width: 100%; height: 60vh; border: 0"/>const frame document.getElementById(iframe);frame.onloa…

Python综合练习之图表

文章目录 文件目录如下图标效果timeline_bar_with_graphic.htmltable_base.html articles.jsonarticlesData.pyarticlesEchartsEntity.pyarticlesEntity.py Python学习了约一个月的时间&#xff0c;这是一篇综合练习的文章。主要做的内容是通过封装对象、实现抽象方法生成统计图…

不可预测的市场中,为何有人持续胜出?

为什么经济学家和证券分析师难以预测经济或股价走势&#xff0c;而少数投资大师却能几十年持续复利&#xff1f;这两个问题看似矛盾&#xff0c;既然无法预测&#xff0c;为何又能产生确定性的赚钱结果呢&#xff1f; 有人认为这是因为幸存者偏差。然而&#xff0c;三十年以上连…

优优嗨聚集团:债务逾期,如何应对与解决?

在现代社会&#xff0c;债务问题已成为越来越多人面临的难题。债务逾期不仅会给个人带来巨大的经济压力&#xff0c;还会影响个人信用记录&#xff0c;甚至可能引发法律纠纷。那么&#xff0c;当债务逾期时&#xff0c;我们应该如何应对与解决呢&#xff1f; 一、了解债务情况 …

14.STL 常用算法

14、STL常用算法 概述&#xff1a; 算法主要是由头文件 、、 组成 是所有STL头文件中最大的一个&#xff0c;范围涉及到比较、 交换、查找、遍历操作、复制、修改等等 体积很小&#xff0c;只包括几个在序列上面进行简单数学运算的模板函数 定义了一些模板类,用以声明函数对象…

1.3 面试经典150题 - 删除有序数组中的重复项

删除有序数组中的重复项 class Solution:def removeDuplicates(self, nums: List[int]) -> int:# 处理边界数据if not nums: return 0if len(nums) 1: return 1# 两个指针&#xff0c;一个记录当前有多少个不重复值的个数&#xff0c;一个记录最新遍历到的值count 1tmp …

C# ObjectArx 绘制表格并设置单元格合并

第一行默认是标题&#xff0c;可设置行【RowType】进行设置类型 Document doc Application.DocumentManager.MdiActiveDocument;using (Transaction tr doc.TransactionManager.StartOpenCloseTransaction()){BlockTable bt tr.GetObject(doc.Database.BlockTableId, OpenMo…

GZ075 云计算应用赛题第9套

2023年全国职业院校技能大赛&#xff08;高职组&#xff09; “云计算应用”赛项赛卷9 某企业根据自身业务需求&#xff0c;实施数字化转型&#xff0c;规划和建设数字化平台&#xff0c;平台聚焦“DevOps开发运维一体化”和“数据驱动产品开发”&#xff0c;拟采用开源OpenSt…

Python3 中常用字符串函数介绍

介绍 Python 中有几个与 字符串数据类型相关的内置函数。这些函数让我们能够轻松修改和操作字符串。我们可以将函数视为在代码元素上执行的操作。内置函数是在 Python 编程语言中定义的&#xff0c;并且可以随时供我们使用的函数。 在本教程中&#xff0c;我们将介绍在 Pytho…

导轨式信号隔离变送器比例阀门线性驱动器4-20mA/0-5V/0-10V转0-165mA/0-80mA/0-1A/0-2A/0-4A

主要特性 精度、线性度误差等级&#xff1a; 0.1、0.2、0.5 级4-20mA/0-5V/0-10V 等标准信号输入0~100mA/0~500mA/0~1A/0-5A 等电流信号输出0~1V(max 2A)/0~10V/0-24V(max 5A) 等电压信号输出信号输入/信号输出 3000VDC 隔离辅助电源&#xff1a;12V、15V 或 24V 直流单电源供…