贪心 · 例13-起床困难综合征(位运算\贪心)

贪心 · 例13-起床困难综合征

#include <bits/stdc++.h>
using namespace std;

signed main(){
    int n, m; cin >> n >> m;
    int base1 = 0, base2 = 0x7fffffff;
    
    for(int i = 1; i <= n; ++i){
        string s; int t; cin >> s >> t;
        if(s[0] == 'A') {base1 &= t; base2 &= t;}
        if(s[0] == 'O') {base1 |= t; base2 |= t;}
        if(s[0] == 'X') {base1 ^= t; base2 ^= t;}
    }

    int ans = 0; 
    for(int i = 30; i >= 0; --i){
        if(base1 & (1 << i)) ans += 1 << i;
        else if(m >= (1 << i) && (base2 & (1 << i))) {m -= 1 << i; ans += 1 << i;}
    }
    
    cout << ans << endl;
    
    return 0;
}
posted @ 2025-03-27 09:40  awei040519  阅读(15)  评论(0)    收藏  举报