https://codeforces.ml/contest/1457/problem/D
Arkady owns a non-decreasing array a1,a2,…,ana1,a2,…,an. You are jealous of its beauty and want to destroy this property. You have a so-called XOR-gun that you can use one or more times.
In one step you can select two consecutive elements of the array, let's say xx and yy, remove them from the array and insert the integer x⊕yx⊕y on their place, where ⊕⊕ denotes the bitwise XOR operation. Note that the length of the array decreases by one after the operation. You can't perform this operation when the length of the array reaches one.
For example, if the array is [2,5,6,8][2,5,6,8], you can select 55 and 66 and replace them with 5⊕6=35⊕6=3. The array becomes [2,3,8][2,3,8].
You want the array no longer be non-decreasing. What is the minimum number of steps needed? If the array stays non-decreasing no matter what you do, print −1−1.
Input
The first line contains a single integer nn (2≤n≤1052≤n≤105) — the initial length of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array. It is guaranteed that ai≤ai+1ai≤ai+1 for all 1≤i<n1≤i<n.
Output
Print a single integer — the minimum number of steps needed. If there is no solution, print −1−1.
Examples
input
Copy
4 2 5 6 8
output
Copy
1
input
Copy
3 1 2 3
output
Copy
-1
input
Copy
5 1 2 4 6 20
output
Copy
2
Note
In the first example you can select 22 and 55 and the array becomes [7,6,8][7,6,8].
In the second example you can only obtain arrays [1,1][1,1], [3,3][3,3] and [0][0] which are all non-decreasing.
In the third example you can select 11 and 22 and the array becomes [3,4,6,20][3,4,6,20]. Then you can, for example, select 33 and 44 and the array becomes [7,6,20][7,6,20], which is no longer non-decreasing.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+10;
const int mod=1e9+7;
ll t,n,cnt,k,p,x,y;
ll a[maxn];
ll flag;
ll b[maxn];
int main()
{
cin>>n;
if(n>=100)
{
cout<<1<<endl;
return 0;
}
b[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=b[i-1]^a[i];
}
ll min1=mod;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
ll l=j-i+1;
ll s=b[j]^b[i-1];
if(i!=1)
{
if(s<a[i-1])
{
min1=min(min1,l);
}
}
if(j!=n)
{
if(s>a[j+1])
{
min1=min(min1,l);
}
}
}
}
if(min1==mod)
{
cout<<-1<<endl;
}
else
cout<<min1-1<<endl;
return 0;
}