zoukankan      html  css  js  c++  java
  • luogu P2114 [NOI2014]起床困难综合症 位运算 二进制

    建议去uoj那里去测,数据比较强

    位运算的题目,就得一位一位的分开考虑

    然后枚举初始值的最高位是0 是1 的最终攻击

    (二进制内)最高位是1肯定比次位是1次次位是1次次次位是1···的大吧,显然

    然后贪心O(N)就能过去啦

    感觉自己是学傻了,看到n=5w就写了个nlog

    情况好像有某一位的初始值是0最终那一位是1,初始值是1,最终那一位也是1的,所以要注意一下

    代码:
    (咦,好像别人家的代码呀)

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <bitset>
    #define ll long long
    using namespace std;
    const int maxn = 1e5 + 7;
    
    int n, m, one, ling, ans, end, cz[maxn], num[maxn];
    
    int read() {
    	int x = 0, f = 1; char s = getchar();
    	for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
    	for (; s <= '9' && s >= '0'; s = getchar()) x = x * 10 + s - '0';
    	return x * f;
    }
    
    int check(int ans) {
    	for (int i = 1; i <= n; ++ i) {
    		if (cz[i] == 1) {
    			ans = ans & num[i];
    		} else if (cz[i] == 2) {
    			ans = ans | num[i];
    		} else if (cz[i] == 3) {
    			ans = ans ^ num[i];
    		}
    	}
    	return ans;
    }
    
    int main() {
    	n = read(), m = read();
    	for (int i = 1; i <= n; ++ i) {
    		char s = getchar();
    		while (s == '
    ' || s == ' ') s = getchar();
    		if (s == 'A') cz[i] = 1;
    		else if (s == 'O') cz[i] = 2;
    		else cz[i] = 3;
    
    		num[i] = read();
    	}
    	one = check((1 << 30) - 1), ling = check(0);
    	for (int i = 30 ; i >= 0; --i) {
    		if (!(ling & (1 << i)) && (one & (1 << i)) && (ans + (1 << i) <= m))
    			ans += 1 << i, end += 1 << i;
    		else if (ling & (1 << i))
    			end += 1 << i;
    
    	}
    	cout << end << "
    ";
    	return 0;
    }
    
  • 相关阅读:
    String,StringBuffer和StringBuilder的异同
    博客迁移到reetsee.com
    一个好用的打印插件,功能强大
    html5中使用标签支持视频播放
    Extjs4 中在指定光标处插入值
    Javascript 创建对象方法的总结
    JS中的prototype
    在JS方法中返回多个值的三种方法
    JS ready和onload事件 比较分析
    JS中的“!!”
  • 原文地址:https://www.cnblogs.com/dsrdsr/p/9752449.html
Copyright © 2011-2022 走看看