Uoj #2 "NOI2014" Get up difficulty syndrome greedy + bitwise arithmetic

Source: Internet
Author: User
Tags bitwise

Topic links

Given the number of n, each number has a specific operation, &|^ three, give a m, the initial value belongs to [0,m], select an initial value, so that all operations done after the value of the largest, output this maximum value.

1, from the highest bit greedy, if the initial this bit is 0, when all operations are finished after the result is 1, then this bit has determined bit 0.

2, initially 0, if this bit is 0 after all operations have been completed, then try the initial condition of 1. Set the current bit is the POS bit, if the previous number plus (1<<pos) is less than m, then this bit is 1 is possible. When the initial is 1, after all operations have been completed, if 1, then this bit is determined to be 1, otherwise this bit is 0.

See the code specifically.

#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };intM, ans, N, digit[100005][ +];Charc[100005][4];voidCalintNumintflag) {    intx =0;  for(inti =0; i<n; i++) {        if(c[i][0] =='A') {x&=Digit[i][num]; } Else if(c[i][0] =='O') {x|=Digit[i][num]; } Else{x^=Digit[i][num]; }    }    if(x) {ans+= (1<<num); return ; }    if(flag) {x=1;  for(inti =0; i<n; i++) {            if(c[i][0] =='A') {x&=Digit[i][num]; } Else if(c[i][0] =='O') {x|=Digit[i][num]; } Else{x^=Digit[i][num]; }        }        if(x = =1) {ans+= (1<<num); M-= (1<<num); }    }}intMain () {intx; CIN>>n>>m;  for(inti =0; i<n; i++) {scanf ("%s%d", C[i], &x); intnum =0;  while(x) {Digit[i][num+ +] = x&1; X>>=1; }    }     for(inti = -; i>=0; i--) {        intFlag =0; if((1<<i) <=m) Flag=1;    Cal (I, flag); } cout<<ans<<Endl; return 0;}

Uoj #2 "NOI2014" Get up difficulty syndrome greedy + bitwise arithmetic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.