UOJ #2 【NOI2014】起床困难综合症

这道题我们设两个bitset(N和Y)

\(N_i = cal(i,0) , Y_i=cal(i,1)\)

cal(i) 即第i位经过题目中的计算后所得出来的值

然后贪心。倒序循环i,考虑第i位如何决策

  • \(N_i\) = 1 , 显然这一位选0好
  • \(Y_i\) = 1 , 那么在不超过限制的情况下,这位应该选1
  • 否则直接选0

倒序循环防止较小数选择过多导致较大数无法被选中导致答案错误

Code:

#include<iostream>
#include<bitset>
#include<cstdio>

using namespace std ;

bitset<50> N,Y ;
int ans = 0 ;
int n,m,res ;
string instr ;

int main(){
	N.reset() , Y.set() ;
	scanf("%d%d",&n,&m) ;
	while(n--){
		int x;
		cin>>instr>>x ;
		if(instr[0] == 'A') N&=x,Y&=x ;
		else if(instr[0] == 'O') N|=x,Y|=x ;
		else N^=x,Y^=x ;
	}	
	for(int i=30;i>=0;--i){
		if((N[i] == 1)) ans += (1<<i) ;
		else if(Y[i]==1 && (res+(1<<i)<=m)) ans+=(1<<i) , res+=(1<<i) ;
	}
	cout<<ans<<endl ;
}

posted @ 2019-05-28 21:48  tyqtyq~!  阅读(128)  评论(0编辑  收藏  举报