CF1763A.Absolute Maximization
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a of length n. You can perform the following operation several (possibly, zero) times:
- Choose i, j, b: Swap the b-th digit in the binary representation of ai and aj.
Find the maximum possible value of 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 0-th bit for 4=1002 and 3=112 will result 1012=5 and 102=2. Swap the 2-nd bit for 4=1002 and 3=112 will result 0002=02=0 and 1112=7.
Here, max(a) denotes the maximum element of array a and min(a) denotes the minimum element of array a.
The binary representation of x is x written in base 2. For example, 9 and 6 written in base 2 are 1001 and 110, respectively.
给你一个长度为 n 的数组 a。你可以执行以下操作若干次(也可以不执行):
- 选择下标 i、j 和位数 b:交换 ai 与 aj 的二进制表示中第 b 位的比特。
求 max(a)−min(a) 的最大可能值。
在二进制表示中,比特位从右(最低有效位)向左(最高有效位)编号。任何二进制表示的开头均视为有无限多个前导零。
例如,对 4=1002 和 3=112 交换第 0 位,结果为 1012=5 和 102=2;对 4=1002 和 3=112 交换第 2 位,结果为 0002=0 和 1112=7。
其中,max(a) 表示数组 a 中的最大元素,min(a) 表示数组 a 中的最小元素。
整数 x 的二进制表示即为 x 在二进制(以 2 为底)下的写法。例如,9 和 6 的二进制表示分别为 1001 和 110。
输入格式
The first line contains a single integer t (1≤t≤128) — the number of testcases.
The first line of each test case contains a single integer n (3≤n≤512) — the length of array a.
The second line of each test case contains n integers a1,a2,…,an (0≤ai<1024) — the elements of array a.
It's guaranteed that the sum of n over all testcases does not exceed 512.
第一行包含一个整数 t(1≤t≤128)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(3≤n≤512)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai<1024)—— 数组 a 的元素。
保证所有测试用例的 n 之和不超过 512。
输出格式
For each testcase, print one integer — the maximum possible value of 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) is 1−0=1.
In the second example, no operation can change the array — the maximum value of max(a)−min(a) is 5−5=0.
In the third example, initially a=[1,2,3,4,5], we can perform one operation taking i=2, j=5, b=1. The array now becomes 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) 的最大值为 1−0=1。
在第二个例子中,任何操作都无法改变该数组——max(a)−min(a) 的最大值为 5−5=0。
在第三个例子中,初始时 a=[1,2,3,4,5],我们可以执行一次操作,取 i=2、j=5、b=1。此时数组变为 a=[1,0,3,4,7]。可以证明,任何进一步的操作均无法得到更优的答案——因此答案为 max(a)−min(a)=7−0=7。
输入解题思路,AI测评打分。不知道怎么写?