版本比较
题解:P16340 「ALFR Round 10」XOR and Simple Trick
正文
增加 14 行删除 14 行
12345567893 行未改动13141516171817181920212223242526272829272829303132333435363736373839404041424343444546479 行未改动575859606161626364657 行未改动73747576777778798081828384858687878889909192929394959629 行未改动[传送门](https://www.luogu.com.cn/problem/P16340)
## 题意简述
给定长度为 $n$ 的非负整数序列 $a$,求有多少对正整数 $(i,j)$ 满足 $1\le i<j\le n$,且对于**任意**非负整数 $x$ 和任意下标 $k$,都有给定长度为 $n$ 的非负整数序列 $a$,求有多少对正整数 $(i,j)$ 满足 $1\le i<j\le n$,且对于**任意**非负整数 $x$ 和任意下标 $k$,都有 $$a_k\oplus x \le a_i\oplus x \quad\text{或}\quad a_k\oplus x \ge a_j\oplus x.$$
换句话说,无论加什么异或偏移 $x$,序列中其他元素异或 $x$ 后的值要么全在 $a_i\oplus x$ 的左边,要么全在 $a_j\oplus x$ 的右边,**不会出现严格介于两者之间的元素**。
3 行未改动
设排序后数组为 $a_1 \le a_2 \le \cdots \le a_n$。
**关键结论**:合法的 $(i,j)$ **一定是排序后的相邻对**,且相邻对 $(i,i+1)$ 合法当且仅当:> 记 $d = \operatorname{highbit}(a_i \oplus a_{i+1})$。> - 若 $i>1$,必须有 $\operatorname{highbit}(a_{i-1} \oplus a_i) \ge d$;> 记 $d = \operatorname{highbit}(a_i \oplus a_{i+1})$。 > - 若 $i>1$,必须有 $\operatorname{highbit}(a_{i-1} \oplus a_i) \ge d$; > - 若 $i+1 < n$,必须有 $\operatorname{highbit}(a_{i+1} \oplus a_{i+2}) \ge d$。
其中 $\operatorname{highbit}(x)$ 表示 $x$ 在二进制下最高位 $1$ 的位置(从 $0$ 开始计数)。
## 推导过程
### 1. 为什么答案只可能出现在相邻对?
考虑任意两个不相邻的数 $a_i, a_j$($i+1<j$),则至少存在一个 $a_k$($i<k<j$)在数值上严格介于两者之间。我们可以构造一个 $x$,使得异或后顺序反转,让 $a_k\oplus x$ 落在 $a_i\oplus x$ 与 $a_j\oplus x$ 之间。具体地,取 $x$ 使得 $a_k\oplus x$ 与两端点比较时,最高不同位分别满足“夹在中间”的条件即可。考虑任意两个不相邻的数 $a_i, a_j$($i+1<j$),则至少存在一个 $a_k$($i<k<j$)在数值上严格介于两者之间。 我们可以构造一个 $x$,使得异或后顺序反转,让 $a_k\oplus x$ 落在 $a_i\oplus x$ 与 $a_j\oplus x$ 之间。 具体地,取 $x$ 使得 $a_k\oplus x$ 与两端点比较时,最高不同位分别满足“夹在中间”的条件即可。 因此不相邻的对必然不合法。
### 2. 相邻对何时合法?
对于相邻对 $(a_i, a_{i+1})$,设 $d = \operatorname{highbit}(a_i \oplus a_{i+1})$,即它们二进制第一个不同的位是第 $d$ 位,且 $a_i$ 在第 $d$ 位为 $0$,$a_{i+1}$ 在第 $d$ 位为 $1$。
当我们异或某个 $x$ 时,所有数的第 $d$ 位可能被翻转。- 若 $x$ 的第 $d$ 位为 $0$,则 $a_i\oplus x$ 的第 $d$ 位仍是 $0$,$a_{i+1}\oplus x$ 的第 $d$ 位仍是 $1$,因此 $a_i\oplus x < a_{i+1}\oplus x$。当我们异或某个 $x$ 时,所有数的第 $d$ 位可能被翻转。 - 若 $x$ 的第 $d$ 位为 $0$,则 $a_i\oplus x$ 的第 $d$ 位仍是 $0$,$a_{i+1}\oplus x$ 的第 $d$ 位仍是 $1$,因此 $a_i\oplus x < a_{i+1}\oplus x$。 此时任何左边的邻居 $a_{i-1}$ 如果满足 $\operatorname{highbit}(a_{i-1}\oplus a_i) < d$,意味着 $a_{i-1}$ 与 $a_i$ 在高于 $d$ 的位上完全相同,只在低于 $d$ 的位上有差异。那么 $a_{i-1}\oplus x$ 会落在 $a_i\oplus x$ 与 $a_{i+1}\oplus x$ 之间(因为第 $d$ 位相同,大小由更低位的比较决定),从而违反条件。
- 若 $x$ 的第 $d$ 位为 $1$,则 $a_i\oplus x$ 的第 $d$ 位变为 $1$,$a_{i+1}\oplus x$ 的第 $d$ 位变为 $0$,此时顺序反转:$a_{i+1}\oplus x < a_i\oplus x$。- 若 $x$ 的第 $d$ 位为 $1$,则 $a_i\oplus x$ 的第 $d$ 位变为 $1$,$a_{i+1}\oplus x$ 的第 $d$ 位变为 $0$,此时顺序反转:$a_{i+1}\oplus x < a_i\oplus x$。 同样,如果右边的邻居 $a_{i+2}$ 满足 $\operatorname{highbit}(a_{i+1}\oplus a_{i+2}) < d$,则 $a_{i+2}\oplus x$ 会落在 $a_{i+1}\oplus x$ 与 $a_i\oplus x$ 之间。
因此,若想让所有 $x$ 都不导致“夹在中间”的情况,就必须满足上述两个不等式。因此,若想让所有 $x$ 都不导致“夹在中间”的情况,就必须满足上述两个不等式。 注意当 $\operatorname{highbit}$ 相等时也不会出现夹在中间,因为相等时意味着两个邻居在该对的两侧方向上有相同的最高差异位,构造的 $x$ 无法让它们跑到中间去。严格推导可知,条件为 $\operatorname{highbit} \ge d$。
### 3. 计算 $\operatorname{highbit}$
9 行未改动 - 若 $i+2<n$ 且 $\operatorname{highbit}(a_{i+1} \oplus a_{i+2}) < d$,则跳过。 - 否则 $ans$ 加一。4. 输出 $ans$。
时间复杂度:排序 $O(n\log n)$,后续扫描 $O(n)$。时间复杂度:排序 $O(n\log n)$,后续扫描 $O(n)$。 空间复杂度:$O(n)$。
## 注意事项
7 行未改动#include <bits/stdc++.h>using namespace std;
// 返回 x 二进制表示中最高位 1 的位置(从0开始计数),x>0inline int high_bit(uint32_t x)inline int high_bit(uint32_t x) { return 31 - __builtin_clz(x); // GCC内建函数,计算前导0个数}
void solve() { ios::sync_with_stdio(false); // 加速cin/cout cin.tie(nullptr); int T; cin >> T; // 数据组数 while(T--) while(T--) { int n; cin >> n; // 序列长度 vector<uint32_t> a(n); for(int i=0;i<n;i++) for(int i=0;i<n;i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组排序29 行未改动