CF1682B.AND Sorting
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation p of integers from 0 to n−1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1 for at least one 1≤i≤n−1 ).
The permutation is called X -sortable for some non-negative integer X if it is possible to sort the permutation by performing the operation below some finite number of times:
- Choose two indices i and j (1≤i<j≤n) such that pi&pj=X .
- Swap pi and pj .
Here & denotes the bitwise AND operation.
Find the maximum value of X such that p is X -sortable. It can be shown that there always exists some value of X such that p is X -sortable.
输入格式
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Description of test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the length of the permutation.
The second line of each test case contains n integers p1,p2,...,pn ( 0≤pi≤n−1 , all pi are distinct) — the elements of p . It is guaranteed that p is not sorted.
It is guaranteed that the sum of n over all cases does not exceed 2⋅105 .
输出格式
For each test case output a single integer — the maximum value of X such that p is X -sortable.
输入输出样例
输入#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 X for which the permutation is X -sortable are X=0 and X=2 , maximum of which is 2 .
Sorting using X=0 :
- Swap p1 and p4 , p=[2,1,3,0] .
- Swap p3 and p4 , p=[2,1,0,3] .
- Swap p1 and p3 , p=[0,1,2,3] .
Sorting using X=2 :
- Swap p3 and p4 , p=[0,1,2,3] .
In the second test case, we must swap p1 and p2 which is possible only with X=0 .