其他分享
首页 > 其他分享> > 【基础】AcWing998 - 起床困难综合症

【基础】AcWing998 - 起床困难综合症

作者:互联网

语法题,注意位操作,一定要加括号,尤其小心比较运算符。注意必须是从高位到低位贪心,假如反过来,有可能导致先加了低位导致再尝试高位时溢出m,而对答案的贡献也是高位更多。

int n, m;
int a[100005];
int x[100005];

int calc(int bit, int k) {
    int bitmask = k;
    for(int i = 1; i <= n; ++i) {
        switch(a[i]) {
        case 'A':
            bitmask &= ((x[i] >> bit) & 1);
            break;
        case 'O':
            bitmask |= ((x[i] >> bit) & 1);
            break;
        case 'X':
            bitmask ^= ((x[i] >> bit) & 1);
            break;
        }
    }
    return (bitmask << bit);
}

void solve() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) {
        char s[10];
        scanf("%s%d", s + 1, &x[i]);
        a[i] = s[1];
    }
    int ans = 0, cur = 0;
    for(int bit = 29; bit >= 0; --bit) {
        int res0 = calc(bit, 0);
        if(res0) {
            ans |= res0;
            continue;
        }
        if((cur | (1 << bit)) > m)
            continue;
        int res1 = calc(bit, 1);
        if(res1) {
            ans |= res1;
            cur |= res1;
            continue;
        }
    }
    printf("%d\n", ans);
    return;
}

标签:int,res1,起床,综合症,bitmask,ans,bit,calc,AcWing998
来源: https://www.cnblogs.com/purinliang/p/13752695.html