CF1416C.XOR Inverse
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 and 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.
给你一个由 n 个非负整数组成的数组 a 。你必须选择一个非负整数 x 并根据以下规则组成一个大小为 n 的新数组 b :对于从 1 到 n 的所有 i , bi=ai⊕x ( ⊕ 表示操作 bitwise XOR)。
b 数组中的反转是一对整数 i 和 j ,即 1≤i<j≤n 和 bi>bj 。
在选择 x 时,应尽量减少 b 中的反转次数。如果 x 有多个选项,则输出最小的一个。
输入格式
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≤ai≤109 ), where ai is the i -th element of a .
第一行包含一个整数 n ( 1≤n≤3⋅105 ) - a 中的元素个数。
第二行包含 n 个空格分隔的整数 a1 , a2 , ..., an ( 0≤ai≤109 ),其中 ai 是 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 的最小值。
输入输出样例
输入#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] .它没有倒位。