BZOJ 3668 [NOI2014] Difficulty getting up syndrome

The meaning of the question: There are some bit operation operations, find an integer less than m so that the number obtained after these operations is the largest.

The operations between each bit of this integer do not affect each other, so we greedily fill in the binary number from the high bit to the position, so that this bit can get 1

#include<cstdio>
using namespace std;
const int N=1e5+5;
int n,m;
int op[N],w[N];
char s[233];
int calc(int x)
{
  for(int i=1;i<=n;i++)
    {
      if(op[i]==1) x&=w[i];
      if(op[i]==2) x|=w[i];
      if(op[i]==3) x^=w[i];
    }
  return x;
}
intmain ()
{
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)
    {
      scanf("%s",s);
      if(s[0]=='A') op[i]=1;
      else if(s[0]=='O') op[i]=2;
      else op[i]=3;
      scanf("%d",&w[i]);
    }
  int u=1,qaq=calc(0);
  for(;u<=m;u<<=1);
  u>>=1;
  int ans=0;
  for(;u;u>>=1)
    {
      if(qaq&u) continue;
      if(ans+u>m) continue;
      if(u&calc(u))ans+=u;
    }
  printf("%d\n",calc(ans));
  return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169291&siteId=291194637