C. XOR Inverse
Description
You are given an array a consisting of n non-negative integers. You have to choose a non-negative integer x and form a new array b of size n according to the following rule: for all i from 1 to n, bi=ai⊕x (⊕ denotes the operation bitwise XOR).
An inversion in the b array is a pair of integers i and j such that 1 ≤ i < j ≤ n 1≤i<j≤n 1≤i<j≤n and b i > b j bi>bj bi>bj.
You should choose x in such a way that the number of inversions in b is minimized. If there are several options for x — output the smallest one.
Input
First line contains a single integer n (1≤n≤3⋅105) — the number of elements in a.
Second line contains n space-separated integers a1, a2, …, an ( 0 ≤ a i ≤ 1 0 9 0≤ai≤10^9 0≤ai≤109), where ai is the i-th element of a.
Output
Output two integers: the minimum possible number of inversions in b, and the minimum possible value of x, which achieves those number of inversions.
Examples
input
4
0 1 3 2
output
1 0
input
9
10 7 9 10 7 5 5 3 5
output
4 14
input
3
8 10 3
output
0 8
Note
In the first sample it is optimal to leave the array as it is by choosing x=0.
In the second sample the selection of x=14 results in b: [4,9,7,4,9,11,11,13,11]. It has 4 inversions:
i=2, j=3;
i=2, j=4;
i=3, j=4;
i=8, j=9.
In the third sample the selection of x=8 results in b: [0,2,11]. It has no inversions.
题意: 指出一个数,这个数与给定的数组中的所有元素做异或运算,生成一个新的数组,使得这个新的数组的逆序对最少。最后输出这个数以及新数组中的逆序对的对数,如果存在多个这样的数则输出最小的那个。
题解:
- 比较两个数的大小 ,其实就是比较两个数的二进制位第一个大小不同的位置的大小,因为字典树可以保存相同的前缀,所以我们先用字典树把这些数字存起来,用 vector 存一下当前前缀相同相同的每个数的位置。
- 考虑每个二进制位 , 因为我们已经记录了这一位为 0 和 1 的数的位置,所以可以直接求出逆序对数 , 这样我们就可以把当前位选 0 还是选 1 的贡献求出来(选 0 的贡献就是逆序对数 ,选 1 的贡献就是正序对数),这里只求出了0和 1 对于全是 0 或 1 的贡献就 可以继续向下递归求。
c++ AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e6 + 7;
int tot = 1, tire[maxn][3];
vector<int> v[maxn];
int n;
int a[maxn];
ll dp[40][3];
ll ans1, ans2;
void insert(int x, int pos) // 构造字典树
{
int p = 0, t;
for (int i = 30; i >= 0; i--)
{
t = (x >> i) & 1;
if (tire[p][t] == 0)
tire[p][t] = tot++;
p = tire[p][t];
v[p].push_back(pos);
}
}
void dfs(int p, int pos)
{
if (pos == -1)
return;
int l = tire[p][0], r = tire[p][1];
ll sz0 = v[l].size(), sz1 = v[r].size();
ll ans = 0, temp = 0;
for (int i = 0; i < sz0; i++)
{
if (!sz1)
break;
while (temp < sz1 && v[r][temp] < v[l][i])
temp++;
ans += temp;
}
dp[pos][0] += ans;
dp[pos][1] += sz1 * sz0 - ans;
if (l != 0)
dfs(l, pos - 1);
if (r != 0)
dfs(r, pos - 1);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
insert(a[i], i);
}
dfs(0, 30);
for (int i = 0; i <= 30; i++)
{
if (dp[i][0] <= dp[i][1])
{
ans1 += dp[i][0];
}
else
{
ans1 += dp[i][1];
ans2 += (1 << i);
}
}
printf("%lld %lld\n", ans1, ans2);
system("pause");
return 0;
}
文章转载自:https://blog.csdn.net/hddddh/article/details/108849708