D. XOR-gun

该博客讨论了一种编程问题,涉及如何使用位异或操作破坏一个非递减数组的性质。给定一个长度为n的非递减数组,任务是通过选择连续元素并用它们的异或值替换来找到使数组不再非递减所需的最少步骤数。文章提供了输入输出示例,并展示了一个可能的解决方案。

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];
        ll u=a[i];
        b[i]=b[i-1]^a[i];
    }
    ll min1=mod;
    for(int i=1;i<=n;i++) {
        for (int j = i; j <= n; j++) {
            for (int ii = j + 1; ii <= n; ii++) {
                for (int jj = ii; jj <= n; jj++) {
                    if((b[j]^b[i-1]) > (b[jj]^b[ii-1])) {
                        //cout<< i <<" "<< j<< " " << ii <<" "<<jj<<endl;
                        //cout<< (b[j]^b[i-1])<< " " <<(b[jj]^b[ii-1])<<endl;
                        //cout<<min1<<endl;
                        min1 = min(min1, 1LL + j - i + jj - ii);
                    }
                }
            }
        }
    }
    if(min1==mod)
    {
        cout<<-1<<endl;
    }
    else
        cout<<min1-1<<endl;
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值