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
.0≤x≤2k−1.
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(1≤i≤n)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](1≤l≤r≤n)
such that
al⊕al+1⊕…⊕ar≠0a
l
⊕
a
l
+
1
⊕
…
⊕
a
r
≠
0al⊕al+1⊕…⊕ar̸=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;
}

本文介绍了一个有趣的贪心算法问题,涉及到数组中k位整数的异或操作。通过维护前缀异或值并利用贪心策略,算法确定了在一系列操作下可以获得的最大数量的喜欢段。关键在于理解异或前缀和的特性,并巧妙地处理0的特殊情况。
609

被折叠的 条评论
为什么被折叠?



