起床困难综合症
有n扇门,从[0,m]之中选一个数字,使他受到的伤害最大
纯暴力时间复杂度是 n * (m + 1),超时
109
二进制下 230
所以可以枚举每一位,得到ans
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n,m; pair<string,int> a[maxn]; int calc(int bit,int now){ for(int i = 0; i < n ;i++){ int x = a[i].second >> bit & 1; if(a[i].first == "AND") now &= x; else if(a[i].first == "OR") now |= x; else now ^= x; } return now; } int main(){ // freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; int ans = 0,val = 0; for(int bit = 29; bit >= 0; bit--){ int res0 = calc(bit,0); int res1 = calc(bit,1); if(val + (1 << bit) <= m && res0 < res1){ val += 1 << bit; ans += res1 << bit; }else ans += res0 << bit; } cout << ans << endl; return 0; }