【LOJ】#2244. 「NOI2014」起床困难综合症

题解

写水题放松一下心情

二进制有个很好的性质是每一位是独立的,我们按位贪心,先看这一位能不能填1,然后看看如果这一位填0那么运算后最后这一位是不是1,是的话就退出,然后看看这一位如果填1最后是1这一位就填1,否则就退出

一次性把所有位填0,所有位填1,然后算两遍,复杂度\(O(n + \log m)\)

代码

#include <bits/stdc++.h>
//#define ivorysi
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define mo 974711
#define MAXN 100005
#define pii pair<int,int>
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {putchar('-');x = -x;}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
int N,M;
char s[MAXN][10];
int num[MAXN];
void Init() {
    read(N);read(M);
    for(int i = 1 ; i <= N ; ++i) {
    scanf("%s",s[i] + 1);read(num[i]);
    }
}
int check(int x) {
    for(int i = 1 ; i <= N ; ++i) {
    if(s[i][1] == 'A') x &= num[i];
    else if(s[i][1] == 'O') x |= num[i];
    else x ^= num[i];
    }
    return x;
}
void Solve() {
    int v0 = check(0),ans = 0,v1 = check((1 << 30) - 1);
    for(int i = 29 ; i >= 0 ; --i) {
    if(ans + (1 << i) > M) continue;
    if(v0 >> i & 1) continue;
    if(v1 >> i & 1) ans |= (1 << i);
    }
    out(check(ans));enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
    return 0;
}

转载于:https://www.cnblogs.com/ivorysi/p/9172870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值