c/c++语言开发共享起床困难综合征(按位贪心)

思路:从0-M中选出一个数使得其求多个位运算后的最大值,位运算当时是按位去算,那么我们要使得最后答案尽可能就从高位向低位枚举看这个位能不能填1,我们首先假设从M中选的是每一位全0和每一位全1,经过几次位运算后,得到最后的答案,通过这个答案我们就可以观察出最后的答案ans的每一位到底是选1还是0,如果此时选0或者1都可以那么选0,(优选选全0状态下此位为1的),如果不行的话,我们看看当m中选出的这个数的这一位是1的时候能不能使答案的这一位是1。#pragma GCC optimize(2)#includ.

起床困难综合征(按位贪心)

思路:从0-M中选出一个数使得其求多个位运算后的最大值,位运算当时是按位去算,那么我们要使得最后答案尽可能就从高位向低位枚举看这个位能不能填1,我们首先假设从M中选的是每一位全0和每一位全1,经过几次位运算后,得到最后的答案,通过这个答案我们就可以观察出最后的答案ans的每一位到底是选1还是0,如果此时选0或者1都可以那么选0,(优选选全0状态下此位为1的),如果不行的话,我们看看当m中选出的这个数的这一位是1的时候能不能使答案的这一位是1。

#pragma GCC optimize(2) #include <cstdio> #include <cstring> #include <algorithm> #include <set> #include<iostream> #include<vector> #include<queue> #include<map> #include<stack> #include<iomanip> #include<cstring> #include<time.h>   using namespace std; typedef long long ll; #define SIS std::ios::sync_with_stdio(false) #define space putchar(' ') #define enter putchar('n') #define lson root<<1 #define rson root<<1|1 typedef pair<int,int> PII; const int mod=1e9+7; const int N=2e6+10; const int M=1e3+10; const int inf=0x3f3f3f3f; const int maxx=2e5+7; const double eps=1e-6; int gcd(int a,int b) {     return b==0?a:gcd(b,a%b); }   ll lcm(ll a,ll b) {     return a*(b/gcd(a,b)); }   template <class T> void read(T &x) {     char c;     bool op = 0;     while(c = getchar(), c < '0' || c > '9')         if(c == '-')             op = 1;     x = c - '0';     while(c = getchar(), c >= '0' && c <= '9')         x = x * 10 + c - '0';     if(op)         x = -x; } template <class T> void write(T x) {     if(x < 0)         x = -x, putchar('-');     if(x >= 10)          write(x / 10);     putchar('0' + x % 10); } ll qsm(int a,int b,int p) {     ll res=1%p;     while(b)     {         if(b&1)             res=res*a%p;         a=1ll*a*a%p;         b>>=1;     }     return res; } ll f0=0,f1=-1;  int main() {     ll n,m;     scanf("%lld%lld",&n,&m);         for(int i=0;i<n;i++)     {         char ch[20];         ll x;         scanf("%s%lld",ch,&x);              if(ch[0]=='A') f0&=x,f1&=x;         else if(ch[0]=='O') f0|=x,f1|=x;         else if(ch[0]=='X') f0^=x,f1^=x;     }    ll ans=0;     for(int i=32;i>=0;i--)     {         if(f0>>i&1) ans+=(1ll<<i);         else if((f1>>i&1ll)&&(1ll<<i)<=m) ans+=(1ll<<i),m-=(1ll<<i);     }     printf("%lldn",ans);                          return 0; }    

c/c++开发分享起床困难综合征(按位贪心)地址:https://blog.csdn.net/qq_43619680/article/details/109270535

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/597116.html

(1)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐