CF1763A.Absolute Maximization

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa of length nn. You can perform the following operation several (possibly, zero) times:

  • Choose ii, jj, bb: Swap the bb-th digit in the binary representation of aia_i and aja_j.

Find the maximum possible value of max⁡(a)−min⁡(a)\max(a) - \min(a).

In a binary representation, bits are numbered from right (least significant) to left (most significant). Consider that there are an infinite number of leading zero bits at the beginning of any binary representation.

For example, swap the 00-th bit for 4=10024=100_2 and 3=1123=11_2 will result 1012=5101_2=5 and 102=210_2=2. Swap the 22-nd bit for 4=10024=100_2 and 3=1123=11_2 will result 0002=02=0000_2=0_2=0 and 1112=7111_2=7.

Here, max⁡(a)\max(a) denotes the maximum element of array aa and min⁡(a)\min(a) denotes the minimum element of array aa.

The binary representation of xx is xx written in base 22. For example, 99 and 66 written in base 22 are 10011001 and 110110, respectively.

给你一个长度为 nn 的数组 aa。你可以执行以下操作若干次(也可以不执行):

  • 选择下标 ii、jj 和位数 bb:交换 aia_i 与 aja_j 的二进制表示中第 bb 位的比特。

求 max⁡(a)−min⁡(a)\max(a) - \min(a) 的最大可能值。

在二进制表示中,比特位从右(最低有效位)向左(最高有效位)编号。任何二进制表示的开头均视为有无限多个前导零。

例如,对 4=10024=100_2 和 3=1123=11_2 交换第 00 位,结果为 1012=5101_2=5 和 102=210_2=2;对 4=10024=100_2 和 3=1123=11_2 交换第 22 位,结果为 0002=0000_2=0 和 1112=7111_2=7。

其中,max⁡(a)\max(a) 表示数组 aa 中的最大元素,min⁡(a)\min(a) 表示数组 aa 中的最小元素。

整数 xx 的二进制表示即为 xx 在二进制(以 22 为底)下的写法。例如,99 和 66 的二进制表示分别为 10011001 和 110110。

输入格式

The first line contains a single integer tt (1≤t≤1281 \le t \le 128) — the number of testcases.

The first line of each test case contains a single integer nn (3≤n≤5123 \le n \le 512) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<10240 \le a_i \lt 1024) — the elements of array aa.

It's guaranteed that the sum of nn over all testcases does not exceed 512512.

第一行包含一个整数 tt(1≤t≤1281 \le t \le 128)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(3≤n≤5123 \le n \le 512)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<10240 \le a_i \lt 1024)—— 数组 aa 的元素。

保证所有测试用例的 nn 之和不超过 512512。

输出格式

For each testcase, print one integer — the maximum possible value of max⁡(a)−min⁡(a)\max(a) - \min(a).

对于每个测试用例,输出一个整数——max⁡(a)−min⁡(a)\max(a) - \min(a) 的最大可能值。

输入输出样例

  • 输入#1

    4
    3
    1 0 1
    4
    5 5 5 5
    5
    1 2 3 4 5
    7
    20 85 100 41 76 49 36

    输出#1

    1
    0
    7
    125

说明/提示

In the first example, it can be shown that we do not need to perform any operations — the maximum value of max⁡(a)−min⁡(a)\max(a) - \min(a) is 1−0=11 - 0 = 1.

In the second example, no operation can change the array — the maximum value of max⁡(a)−min⁡(a)\max(a) - \min(a) is 5−5=05 - 5 = 0.

In the third example, initially a=[1,2,3,4,5]a = [1, 2, 3, 4, 5], we can perform one operation taking i=2i = 2, j=5j = 5, b=1b = 1. The array now becomes a=[1,0,3,4,7]a = [1, 0, 3, 4, 7]. It can be shown that any further operations do not lead to a better answer — therefore the answer is max⁡(a)−min⁡(a)=7−0=7\max(a) - \min(a) = 7 - 0 = 7.

在第一个例子中,可以证明我们无需执行任何操作——max⁡(a)−min⁡(a)\max(a) - \min(a) 的最大值为 1−0=11 - 0 = 1。

在第二个例子中,任何操作都无法改变该数组——max⁡(a)−min⁡(a)\max(a) - \min(a) 的最大值为 5−5=05 - 5 = 0。

在第三个例子中,初始时 a=[1,2,3,4,5]a = [1, 2, 3, 4, 5],我们可以执行一次操作,取 i=2i = 2、j=5j = 5、b=1b = 1。此时数组变为 a=[1,0,3,4,7]a = [1, 0, 3, 4, 7]。可以证明,任何进一步的操作均无法得到更优的答案——因此答案为 max⁡(a)−min⁡(a)=7−0=7\max(a) - \min(a) = 7 - 0 = 7。

输入解题思路,AI测评打分。不知道怎么写?

首页