#P9696. [GDCPC2023] Swapping Operation

[GDCPC2023] Swapping Operation

题目描述

Given a non-negative integer sequence A=a1,a2,,anA = a_1, a_2, \dots, a_n of length nn, define

$$F(A)=\max\limits_{1\leq k<n} ((a_1 \,\&\, a_2 \,\&\, \cdots \,\&\, a_k)+(a_{k+1} \,\&\, a_{k+2} \,\&\, \cdots \,\&\, a_n)) $$

where &\& is the bitwise-and operator.

You can perform the swapping operation at most once: choose two indices ii and jj such that 1i<jn1\leq i < j\leq n and then swap the values of aia_i and aja_j.

Calculate the maximum possible value of F(A)F(A) after performing at most one swapping operation.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (2n1052\leq n\leq 10^5) indicating the length of sequence AA.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (0ai1090\leq a_i\leq 10^9) indicating the given sequence AA.

It's guaranteed that the sum of nn of all test cases will not exceed 10510^5.

输出格式

For each test case output one line containing one integer indicating the maximum possible value of F(A)F(A) after performing at most one swapping operation.

题目大意

【题目描述】

给定长度为 nn 的非负整数序列 A=a1,a2,,anA = a_1, a_2, \dots, a_n,定义

$$F(A)=\max\limits_{1\leq k<n} ((a_1 \,\&\, a_2 \,\&\, \cdots \,\&\, a_k)+(a_{k+1} \,\&\, a_{k+2} \,\&\, \cdots \,\&\, a_n)) $$

其中 &\& 表示按位与操作。

您可以进行至多一次交换操作:选择两个下标 iijj 满足 1i<jn1\leq i < j\leq n,交换 aia_iaja_j 的值。

求经过至多一次交换后,F(A)F(A) 的最大值。

【输入格式】

有多组测试数据。第一行输入一个整数 TT 表示测试数据组数。对于每组测试数据:

第一行输入一个整数 nn2n1052\leq n\leq 10^5),表示序列 AA 的长度。

第二行输入 nn 个整数 a1,a2,,ana_1, a_2, \cdots, a_n0ai1090\leq a_i\leq 10^9),表示给定的序列 AA

保证所有数据 nn 之和不超过 10510^5

【输出格式】

每组数据输出一行一个整数,表示经过至多一次交换后 F(A)F(A) 的最大值。

【样例解释】

对于第一组样例数据,可以交换 a4a_4a6a_6 将序列变为 {6,5,4,6,5,3}\{6, 5, 4, 6, 5, 3\},然后选择 k=5k = 5,就得到了 $F(A) = (6 \,\&\, 5 \,\&\, 4 \,\&\, 6 \,\&\, 5) + (3) = 7$。

对于第二组样例数据,可以交换 a2a_2a4a_4 将序列变为 {1,1,1,2,2,2}\{1, 1, 1, 2, 2, 2\},然后选择 k=3k = 3,就得到了 $F(A) = (1 \,\&\, 1 \,\&\, 1) + (2 \,\&\, 2 \,\&\, 2) = 3$。

对于第三组样例数据,不进行交换操作,然后选择 k=2k = 2,就得到了 F(A)=(1&1)+(2&2&2)=3F(A) = (1 \,\&\, 1) + (2 \,\&\, 2 \,\&\, 2) = 3

3
6
6 5 4 3 5 6
6
1 2 1 1 2 2
5
1 1 2 2 2
7
3
3

提示

For the first sample test case, we can swap a4a_4 and a6a_6 so the sequence becomes {6,5,4,6,5,3}\{6, 5, 4, 6, 5, 3\}. We can then choose k=5k = 5 so that $F(A) = (6 \,\&\, 5 \,\&\, 4 \,\&\, 6 \,\&\, 5) + (3) = 7$.

For the second sample test case, we can swap a2a_2 and a4a_4 so the sequence becomes {1,1,1,2,2,2}\{1, 1, 1, 2, 2, 2\}. We can then choose k=3k = 3 so that $F(A) = (1 \,\&\, 1 \,\&\, 1) + (2 \,\&\, 2 \,\&\, 2) = 3$.

For the third sample test case we do not perform the swapping operation. We can then choose k=2k = 2 so that F(A)=(1&1)+(2&2&2)=3F(A) = (1 \,\&\, 1) + (2 \,\&\, 2 \,\&\, 2) = 3.