题解 P6824 【「EZEC-4」可乐】
提供一种容易想到(?)的想法。
先把可乐从小到大排序,答案肯定是其中一段。
首先枚举聪明值
对于每一位,设这一位为
设
如果
如果
用了
#include <bits/stdc++.h>
#define R register
using namespace std;
const int N = 1e6+5;
int n, k, a[N], ans;
int read(){
char ch=getchar();int x=0,f=1;
while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main(){
// std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.out", "w", stdout);
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
sort(a+1, a+1+n);
for (R int x = 0; x <= (1<<20); x++){
R int sum = 0, l = 1, r = n, now = 0;
for (R int i = 20; i >= 0; i--){
R int pos = lower_bound(a+l, a+r+1, now|(1<<i))-a;
if (!((k>>i)&1)){
if ((x>>i)&1) l = pos, now |= (1<<i);//1^1==0,1在右边,向右走
else r = pos-1;//0^0==0,0在左边,向左走
}else{
if ((x>>i)&1) sum += r-pos+1, r = pos-1;//1^1==0,右边必然<k,计入答案,抛弃
else sum += pos-l, l = pos, now |= (1<<i);//同理
}//now就是从高位走过来的路
}
ans = max(sum, ans);
}
cout << ans;
return 0;
}