CF1682B.AND Sorting

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation pp of integers from 00 to n−1n-1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1p_i \gt p_{i+1} for at least one 1≤i≤n−11 \le i \le n - 1).

The permutation is called XX-sortable for some non-negative integer XX if it is possible to sort the permutation by performing the operation below some finite number of times:

  • Choose two indices ii and jj (1≤i<j≤n)(1 \le i \lt j \le n) such that pi&pj=Xp_i \& p_j = X.
  • Swap pip_i and pjp_j.

Here &\& denotes the bitwise AND operation.

Find the maximum value of XX such that pp is XX-sortable. It can be shown that there always exists some value of XX such that pp is XX-sortable.

给你一个由 00 到 n−1n-1 的整数构成的排列 pp(每个整数恰好出现一次)。初始时,该排列未排序(即至少存在一个下标 1≤i≤n−11 \le i \le n - 1,使得 pi>pi+1p_i \gt p_{i+1})。

对于某个非负整数 XX,若可通过有限次执行以下操作将该排列排序,则称该排列是 XX-可排序的:

  • 选择两个下标 ii 和 jj(满足 1≤i<j≤n1 \le i \lt j \le n),使得 pi&pj=Xp_i \& p_j = X;
  • 交换 pip_i 和 pjp_j。

其中 &\& 表示按位与运算。

请找出最大的 XX 值,使得 pp 是 XX-可排序的。可以证明:总存在某个 XX 值使得 pp 是 XX-可排序的。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤104)(1 \le t \le 10^4) — the number of test cases. Description of test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅105)(2 \le n \le 2 \cdot 10^5) — the length of the permutation.

The second line of each test case contains nn integers p1,p2,...,pnp_1, p_2, ..., p_n (0≤pi≤n−10 \le p_i \le n-1, all pip_i are distinct) — the elements of pp. It is guaranteed that pp is not sorted.

It is guaranteed that the sum of nn over all cases does not exceed 2⋅1052 \cdot 10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5),表示排列的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,...,pnp_1, p_2, ..., p_n(0≤pi≤n−10 \le p_i \le n-1,且所有 pip_i 互不相同),即排列 pp 的元素。保证 pp 不是升序排列的。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case output a single integer — the maximum value of XX such that pp is XX-sortable.

对于每个测试用例,输出一个整数——使得 pp 是 XX-可排序的最大 XX 值。

输入输出样例

  • 输入#1

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

    输出#1

    2
    0
    4
    1

说明/提示

In the first test case, the only XX for which the permutation is XX-sortable are X=0X = 0 and X=2X = 2, maximum of which is 22.

Sorting using X=0X = 0:

  • Swap p1p_1 and p4p_4, p=[2,1,3,0]p = [2, 1, 3, 0].
  • Swap p3p_3 and p4p_4, p=[2,1,0,3]p = [2, 1, 0, 3].
  • Swap p1p_1 and p3p_3, p=[0,1,2,3]p = [0, 1, 2, 3].

Sorting using X=2X = 2:

  • Swap p3p_3 and p4p_4, p=[0,1,2,3]p = [0, 1, 2, 3].

In the second test case, we must swap p1p_1 and p2p_2 which is possible only with X=0X = 0.

在第一个测试用例中,使得该排列为 XX-可排序的唯一 XX 值是 X=0X = 0 和 X=2X = 2,其中最大值为 22。

使用 X=0X = 0 进行排序:

  • 交换 p1p_1 与 p4p_4,得到 p=[2,1,3,0]p = [2, 1, 3, 0]。
  • 交换 p3p_3 与 p4p_4,得到 p=[2,1,0,3]p = [2, 1, 0, 3]。
  • 交换 p1p_1 与 p3p_3,得到 p=[0,1,2,3]p = [0, 1, 2, 3]。

使用 X=2X = 2 进行排序:

  • 交换 p3p_3 与 p4p_4,得到 p=[0,1,2,3]p = [0, 1, 2, 3]。

在第二个测试用例中,我们必须交换 p1p_1 与 p2p_2,而这仅当 X=0X = 0 时才可行。

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

首页