省选专练之CF1054D. Changing Array

本文介绍了一种利用贪心算法解决特定序列异或问题的方法。通过维护前缀异或值,算法能有效确定最大数量的喜欢段落,其中每个段落的异或结果不为零。文章详细解释了算法思路,并提供了完整的C++代码实现。

outputstandard output
At a break Vanya came to the class and saw an array of
nkn knk-bit integers a1,a2,…,ana 1 , a 2 , … , a na1,a2,,anon the board. An integer xxxis called a kkk
-bit integer if
0≤x≤2k−1.0 ≤ x ≤ 2 k − 1 .0x2k1.

Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array i(1≤i≤n)i ( 1 ≤ i ≤ n )i(1in)and replace the number aia iai
with the number
¯¯¯¯¯¯¯¯¯¯¯¯
aia iai. We define
¯¯¯¯¯¯¯¯¯xxx for a kkk-bit integer xxxas the kkk-bit integer such that all its kkkbits differ from the corresponding bits of xxx
Vanya does not like the number
000 Therefore, he likes such segments
[l,r](1≤l≤r≤n)[ l , r ] ( 1 ≤ l ≤ r ≤ n )[l,r](1lrn)
such that
al⊕al+1⊕…⊕ar≠0a l ⊕ a l + 1 ⊕ … ⊕ a r ≠ 0alal+1ar̸=0, where ⊕⊕denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.

有趣的贪心。
我们发现一段序列的异或前缀和实际只有两种。
因为你选择异或的值是永远一样的。
维护前缀值。
注意000的情况。

#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int,int>mmp;
int pre=0;
int n,k,Mx;
int ans=0;
signed main(){
	cin>>n>>k;
	Mx=(1<<k)-1;
	ans=n*(int)(n+1)/2;
	mmp[0]=1;
	for(int i=1;i<=n;++i){
		int x;
		cin>>x;
		int A=pre^x;
		int B=A^Mx;
		if(!mmp.count(A))mmp[A]=0;
		if(!mmp.count(B))mmp[B]=0;
		if(mmp[A]<=mmp[B]){
			ans-=mmp[A];
			pre=A;
			mmp[A]++;
		}
		else{
			ans-=mmp[B];
			pre=B;
			mmp[B]++;
		}
	}
	cout<<ans;
}

转载于:https://www.cnblogs.com/Leo-JAM/p/10079060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值