codevs 3311 [NOI2014] 起床困难综合症 贪心

18 篇文章 0 订阅

题目:
http://codevs.cn/problem/3311/

各种玄学WA,最后把数组调大,然后从第31位开始拆分就过了

为啥开long long从第32位拆分会WA?如果有dalao知道,请留言,谢谢;

贪心:

从高位到低位,如果选0和1都一样,那么选0,将”机会”留给较小位的1;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll MAXN=3000001;
string s;
ll a[MAXN],num[MAXN];
ll n,m,ans;

ll calc(ll x)
{
    for(ll i=1;i<=n;i++)
    {
        if(a[i]==1) x=(x&num[i]);
        else if(a[i]==2) x=(x|num[i]);
        else x=(x^num[i]);
    }
    return x;
}

void solve()
{
    scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=n;i++)
    {
        cin>>s,scanf("%lld",&num[i]);
        if(s=="AND") a[i]=1;
        else if(s=="OR") a[i]=2;
        else a[i]=3;
    }
    ll t=calc(0);
    for(ll i=31;i>=0;i--)
    {
        if(ans+(1<<i)>m)//如果超了直接跳过;
        continue;
        else if(calc(1<<i)>t)
            ans+=(1<<i);
    }
    cout<<calc(ans)<<'\n';
    return;
}

int main()
{
    solve();
    return 0; 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值