A13866 | XOR Inverse
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ , $b_i = a_i \oplus x$ ( $\oplus$ denotes the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)).
An inversion in the $b$ array is a pair of integers $i$ and $j$ such that $1 \le i < j \le n$ and $b_i > b_j$ .
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.
给你一个由 $n$ 个非负整数组成的数组 $a$ 。你必须选择一个非负整数 $x$ 并根据以下规则组成一个大小为 $n$ 的新数组 $b$ :对于从 $1$ 到 $n$ 的所有 $i$ , $b_i = a_i \oplus x$ ( $\oplus$ 表示操作 [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR))。
$b$ 数组中的反转是一对整数 $i$ 和 $j$ ,即 $1 \le i \lt j \le n$ 和 $b_i \gt b_j$ 。
在选择 $x$ 时,应尽量减少 $b$ 中的反转次数。如果 $x$ 有多个选项,则输出最小的一个。
An inversion in the $b$ array is a pair of integers $i$ and $j$ such that $1 \le i < j \le n$ and $b_i > b_j$ .
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.
给你一个由 $n$ 个非负整数组成的数组 $a$ 。你必须选择一个非负整数 $x$ 并根据以下规则组成一个大小为 $n$ 的新数组 $b$ :对于从 $1$ 到 $n$ 的所有 $i$ , $b_i = a_i \oplus x$ ( $\oplus$ 表示操作 [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR))。
$b$ 数组中的反转是一对整数 $i$ 和 $j$ ,即 $1 \le i \lt j \le n$ 和 $b_i \gt b_j$ 。
在选择 $x$ 时,应尽量减少 $b$ 中的反转次数。如果 $x$ 有多个选项,则输出最小的一个。
输入格式
First line contains a single integer $n$ ( $1 \le n \le 3 \cdot 10^5$ ) — the number of elements in $a$ .
Second line contains $n$ space-separated integers $a_1$ , $a_2$ , ..., $a_n$ ( $0 \le a_i \le 10^9$ ), where $a_i$ is the $i$ -th element of $a$ .
第一行包含一个整数 $n$ ( $1 \le n \le 3 \cdot 10^5$ ) - $a$ 中的元素个数。
第二行包含 $n$ 个空格分隔的整数 $a_1$ , $a_2$ , ..., $a_n$ ( $0 \le a_i \le 10^9$ ),其中 $a_i$ 是 $a$ 的 $i$ -th 元素。
Second line contains $n$ space-separated integers $a_1$ , $a_2$ , ..., $a_n$ ( $0 \le a_i \le 10^9$ ), where $a_i$ is the $i$ -th element of $a$ .
第一行包含一个整数 $n$ ( $1 \le n \le 3 \cdot 10^5$ ) - $a$ 中的元素个数。
第二行包含 $n$ 个空格分隔的整数 $a_1$ , $a_2$ , ..., $a_n$ ( $0 \le a_i \le 10^9$ ),其中 $a_i$ 是 $a$ 的 $i$ -th 元素。
输出格式
Output two integers: the minimum possible number of inversions in $b$ , and the minimum possible value of $x$ , which achieves those number of inversions.
输出两个整数: $b$ 中可能的最小反转次数,以及实现这些反转次数的 $x$ 的最小值。
输出两个整数: $b$ 中可能的最小反转次数,以及实现这些反转次数的 $x$ 的最小值。
输入输出样例
输入 #1
4 0 1 3 2
输出 #1
1 0
输入 #2
9 10 7 9 10 7 5 5 3 5
输出 #2
4 14
输入 #3
3 8 10 3
输出 #3
0 8
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.
在第一个样本中,选择 $x = 0$ 是保持数组原样的最佳选择。
在第二个样本中,选择 $x = 14$ 的结果是 $b$ : $[4, 9, 7, 4, 9, 11, 11, 13, 11]$ .反转次数为 $4$ :
- $i = 2$ , $j = 3$ ;
- $i = 2$ , $j = 4$ ;
- $i = 3$ , $j = 4$ ;
- $i = 8$ , $j = 9$ .
在第三个样本中,选择 $x = 8$ 的结果是 $b$ : $[0, 2, 11]$ .它没有倒位。
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.
在第一个样本中,选择 $x = 0$ 是保持数组原样的最佳选择。
在第二个样本中,选择 $x = 14$ 的结果是 $b$ : $[4, 9, 7, 4, 9, 11, 11, 13, 11]$ .反转次数为 $4$ :
- $i = 2$ , $j = 3$ ;
- $i = 2$ , $j = 4$ ;
- $i = 3$ , $j = 4$ ;
- $i = 8$ , $j = 9$ .
在第三个样本中,选择 $x = 8$ 的结果是 $b$ : $[0, 2, 11]$ .它没有倒位。
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted