首页 > 其他 > 详细

起床困难综合症

时间:2020-09-03 21:03:26      阅读:61      评论:0      收藏:0      [点我收藏+]

链接

有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;
}
View Code

 

起床困难综合症

原文:https://www.cnblogs.com/xcfxcf/p/13610295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!