Technocup 2021 - Elimination Round 2 D. XOR-gun

185 篇文章 0 订阅

Arkady owns a non-decreasing array 𝑎1,𝑎2,…,𝑎𝑛. 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 𝑥 and 𝑦, remove them from the array and insert the integer 𝑥⊕𝑦 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 𝑛 (2≤𝑛≤105) — the initial length of the array.

The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the elements of the array. It is guaranteed that 𝑎𝑖≤𝑎𝑖+1 for all 1≤𝑖<𝑛.

Output
Print a single integer — the minimum number of steps needed. If there is no solution, print −1.

Examples
inputCopy
4
2 5 6 8
outputCopy
1
inputCopy
3
1 2 3
outputCopy
-1
inputCopy
5
1 2 4 6 20
outputCopy
2

分析:考虑解的一般形式,必定是由一段连续的区间产生,且若区间长度为L则答案就应该是L-2,设L最后合并为两个元素a,b 则 a > b,考虑a ^ b 的二进制最高位1,可以知道a的二进制表示中这个位置一定也为1,且b中对应位置一定为0,这就启发我们可以考虑去枚举L,设L的区间异或和为c,对于c的最高位1,如果它在L的某个真前缀出现过,那么这个前缀对应的前缀异或就可以作为上边结论中的a,其对应的后缀就是结论中的b,此时一定有a > b,否则和最高位的假设矛盾,通过这个结论我们可以知道如果固定区间的起始位置,则随着区间长度L的增加新的区间异或合的最高位1如果一旦在前边的某个前缀和中出现过则我们就找到了一组合法解,题目中a[i]最多只有30位所以答案一定不会很大,直接暴力枚举起始为止就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值