【贪心】BZOJ3668 [Noi2014]起床困难综合症

本文介绍了一种解决特定问题的按位贪心算法,并提供了一个示例程序。该算法通过对每一位进行判断来逐步构建最优解,适用于位运算操作如或、与、异或等混合场景。

题面在这里

按位贪心就好了

示例程序:

#include<cstdio>

const int maxn=100005;
int n,m,a[maxn],ans=0,now=0;
char op[maxn];
inline int red(){
    int res=0;char ch=getchar();
    while (ch<'0'||'9'<ch) ch=getchar();
    while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=getchar();
    return res;
}
inline char fstchar(){
    char ch=getchar();
    while (ch<'A'||'Z'<ch) ch=getchar();
    return ch;
}
inline bool w(int l,int x){
    int res=x<<l;
    for (int i=1;i<=n;i++)
     if (op[i]=='O') res|=a[i];else
     if (op[i]=='A') res&=a[i];else
     res^=a[i];
    return (res>>l)&1;
}
int main(){
    n=red(),m=red();
    for (int i=1;i<=n;i++) op[i]=fstchar(),a[i]=red();
    for (int i=30;i>=0;i--)
     if (w(i,0)) ans+=(1<<i);else
     if (w(i,1)&&now+(1<<i)<=m) now+=(1<<i),ans+=(1<<i);
    printf("%d",ans);
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值