BZOJ4245 [ONTAK2015]OR-XOR 【贪心】
题目链接
题解
套路①
位运算当然要分位讨论,高位优先
考虑在\(or\)下,如果该位为\(0\),则每一位都为\(0\)
套路②
我们选m段异或和,转化为\(m\)个前缀和的点,且其中有一个是\(n\)
容易发现,该位结果要为0,则选取的前缀和该位都为\(0\)
所以贪心查找所有该位为\(0\)的,首先第\(n\)个前缀和一定要为\(0\),如果其它满足有至少\(m - 1\)个,那么该位答案为\(0\),剩余为\(1\)的打上标记不能选
如果不够,那这一位就没办法了,直接放弃
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 100005,INF = 1000000000;
inline LL read(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,tag[maxn];
LL a[maxn],ans;
int main(){
n = read(); m = read();
REP(i,n) a[i] = read() ^ a[i - 1];
for (LL i = 1ll << 61; i; i >>= 1){
if (a[n] & i){
ans += i;
continue;
}
int cnt = 1;
for (int j = 1; j < n; j++)
if (!tag[j] && !(a[j] & i)) cnt++;
if (cnt >= m){
for (int j = 1; j < n; j++)
if (!tag[j] && (a[j] & i))
tag[j] = true;
}
else ans += i;
}
printf("%lld\n",ans);
return 0;
}