XOR-gun

Arkady owns a non-decreasing array a1,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 x and y, remove them from the array and insert the integer x⊕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], you can select 5 and 6 and replace them with 5⊕6=3. The array becomes [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.

Input
The first line contains a single integer n (2≤n≤105) — the initial length of the array.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array. It is guaranteed that ai≤ai+1 for all 1≤i<n.

Output
Print a single integer — the minimum number of steps needed. If there is no solution, print −1.
Examples
input
4
2 5 6 8
output
1
input
3
1 2 3
output
-1
input
5
1 2 4 6 20
output
2
Note
In the first example you can select 2 and 5 and the array becomes [7,6,8].

In the second example you can only obtain arrays [1,1], [3,3] and [0] which are all non-decreasing.

In the third example you can select 1 and 2 and the array becomes [3,4,6,20]. Then you can, for example, select 3 and 4 and the array becomes [7,6,20], which is no longer non-decreasing.
转换为二进制,当三个连续数的最高位相同时,则必定可以破坏非递减数列。
a[i]<=1e9,则二进制最多占用30位,所以当有大于60个数字的时候由鸽巢原理得一定有三个连续的数字最高位相同这时结果为1
小于60,暴力枚举左右区间,异或后的大小关系。

#include <stdio.h>
#define min(a, b) (a < b ? a : b)
int a[1000001];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
#endif
    int i, j, n, x, lz, lr;
    scanf("%d", &n);
    for (i = 1; i < n + 1; i++)
    {
        scanf("%d", &a[i]);
        a[i] ^= a[i - 1];
    }
    if (n > 60)
    {
        printf("1\n");
    }
    else
    {
        int ans = 1000001;
        for (i = 1; i <= n; i++)
        {
            for (j = i; j <= n; j++)
            {
                x = j + 1;
                for (x; x <= n; x++)
                {
                    lz = a[j] ^ a[i - 1];
                    lr = a[x] ^ a[j];
                    if (lz > lr)
                    {
                        ans = min(ans, x - i - 1);
                    }
                }
            }
        }
        if (ans == 1000001)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n", ans);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值