BZOJ 3668 [Noi2014]起床困难综合症

题解:贪心,从高位到低位考虑这一位到底是1还是0

O(nlogn)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100009;

int n,m;
int ans;
int a[maxn];
int op[maxn];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		char s[10];
		scanf("%s",s);
		scanf("%d",&a[i]);
		if(s[0]=='A'){
			op[i]=1;
		}
		if(s[0]=='O'){
			op[i]=2;
		}
		if(s[0]=='X'){
			op[i]=3;
		}
	}
	for(int j=30;j>=1;--j){
		int b1=0,b2=1;
		for(int i=1;i<=n;++i){
			if(op[i]==1){
				if(a[i]&(1<<(j-1))){
					b1&=1;b2&=1;
				}else{
					b1&=0;b2&=0;
				}
			}
			if(op[i]==2){
				if(a[i]&(1<<(j-1))){
					b1|=1;b2|=1;
				}else{
					b1|=0;b2|=0;
				}
			}
			if(op[i]==3){
				if(a[i]&(1<<(j-1))){
					b1^=1;b2^=1;
				}else{
					b1^=0;b2^=0;
				}
			}
		}
		if(b1&&b2){
			ans+=(1<<(j-1));
		}else if(b1){
			ans+=(1<<(j-1));
		}else if(b2){
			if(m>=(1<<(j-1))){
				m-=(1<<(j-1));
				ans+=(1<<(j-1));
			}else{
			}
		}else{
		}
	}
	
	printf("%d\n",ans);
	return 0;
}

  

posted @ 2018-03-02 18:27  ws_zzy  阅读(148)  评论(0编辑  收藏  举报