[BZOJ4245][ONTAK2015]OR-XOR(贪心)

有关or的最大/小值问题,一般都是从大往小贪心,后面的贪心不破坏前面贪心的结果。

对于这题同样从大往小按位考虑,只需要考虑前缀异或和。显然要使这一位为0,每段的末尾的前缀异或和必须均为0,且n的前缀异或和也为0。

如果这位上为0的前缀个数不到m则放弃这一位,否则将所有这一位为1的位置删去。最终结果一定是所求最小值。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 4 typedef long long ll;
 5 using namespace std;
 6 
 7 const int N=1000010;
 8 int n,m;
 9 ll ans,a[N];
10 
11 int main(){
12     freopen("bzoj4245.in","r",stdin);
13     freopen("bzoj4245.out","w",stdout);
14     scanf("%d%d",&n,&m);
15     rep(i,1,n) scanf("%lld",&a[i]),a[i]^=a[i-1];
16     for (int d=60; ~d; d--){
17         int cnt=0;
18         if (a[n]&(1ll<<d)) { ans|=1ll<<d; continue; }
19         rep(i,1,n) if (~a[i] && !(a[i]&(1ll<<d))) cnt++;
20         if (cnt<m){ ans|=1ll<<d; continue; }
21         rep(i,1,n) if (~a[i] && a[i]&(1ll<<d)) a[i]=-1;
22     }
23     printf("%lld\n",ans);
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/HocRiser/p/9890250.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值