思路:按位贪心。
提交:1次
题解:
可以先处理出对于全$0$串和全$1$串最后每一位的结果。(每一位 从 $0$ $or$ $1$ 变成 $0$ $or$ $1$)
对于每一位,若不能变成$1$,则不选;
若可以变成$1$,优先让$0$变成$1$(使代价尽量小);若不能让$0$变成$1$,则$1$变成$1$(不变$qwq$)
代码:
#include<cstdio> #include<iostream> using namespace std; #define ull unsigned long long #define ll long long #define R register int #define pause (for(R i=1;i<=10000000000;++i)) #define In freopen("NOIPAK++.in","r",stdin) #define Out freopen("out.out","w",stdout) namespace Fread { static char B[1<<15],*S=B,*D=B; #ifndef JACK #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) #endif inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch==‘-‘?-1:fix; if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);} inline void gs(char* s) { register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar())); } } using Fread::g; using Fread::gs; namespace Luitaryi { int n,m,c1,c2=-1,ans; inline void main() { n=g(),m=g(); for(R i=1,x;i<=n;++i) { register char ch[4]; gs(ch),x=g(); if(ch[0]==‘A‘) c1&=x,c2&=x; else if(ch[0]==‘O‘) c1|=x,c2|=x; else if(ch[0]==‘X‘) c1^=x,c2^=x; } for(R i=29;~i;--i) { if(c1&(1<<i)) ans+=1<<i; else if(c2&(1<<i)&&(1<<i)<=m) ans+=1<<i,c2-=1<<i; } printf("%d\n",ans); } } signed main() { Luitaryi::main(); return 0; }
2019.07.14
原文:https://www.cnblogs.com/Jackpei/p/11186429.html