Cow XOR
Adrian Vladu -- 2005
Farmer John is stuck with another problem while feeding his cows. all of his n (1 ≤ n ≤ 100,000) cows (numbered 1 .. n) are lined up in front of the barn, sorted by their rank in their social hierarchy. cow #1 has
The highest rank; cow # N has the least rank. every cow had additionally been assigned a non-unique integer number in the range 0 .. (221-1 ).
Help FJ choose which cows will be fed first by selecting a sequence of consecutive cows in the line such that the bitwise "XOR" between their assigned numbers has the maximum value. if there are several such sequences,
Choose the sequence for which its last cow has the highest rank. if there still is a tie, choose the shortest sequence.
Time Limit: 0.5 secprogram name: cowxorinput format
- Line 1: A single integer n
- Lines 2. n + 1: N integers ranging from 0 to 221-1, representing the cows 'assigned numbers. Line J describes cow of social hierarchy J-1.
Sample input (File cowxor. In)
510542
Input details:
There are 5 cows. Cow #1 had been assigned with 1; cow #2 with 0; cow #3 with 5; cow #4 with 4; cow #5 with 2.
Output Format
- Line 1: three space-separated integers, respectively: the maximum requested value, the position where the sequence begins, the position where the sequence ends.
Sample output (File cowxor. out)
6 4 5
Output details:
4 XOR 2 = 6 (001) XOR (010) = (011)
Question: Give You A series with a length of N, and find a continuous sub-series, so that all the numbers of this Sub-series are exclusive or the maximum...
Analysis: The first thing we can think of is to enumerate each sub-series in brute force mode, and then calculate the answer. This definitely times out. All optimization is to take advantage of the exclusive or nature, assuming
Bit [I] = x [1] ^ X [2] ^... ^ X [I]
So, interval I ~ The value of J is bit [I-1] ^ bit [J]
Convert the original question to two numbers to maximize the exclusive or value...
We can enumerate a number and construct another number to maximize the current answer.
How to construct it? Assume the current bit [I] = 001010
From the high position, determine whether the bit starting with 1 exists. If it exists, this bit is 1, and if it does not exist, it is 0.
If yes, determine whether the bits starting with 11 exist. If yes, continue to judge the bits starting with 110...
Now there is one problem. How can we determine whether the beginning of BIT in front of I exists? Many solutions are saved using tire. In fact, everything is the same, because the binary system is special, you can directly use a bool array to save whether the status exists. If the status exists at the beginning of 11, the value is assigned to 1 directly. However, if 0000 exists, it is the same as 0, and an error occurs, add 1 before 11, that is, 11-> 111,00-> 100
Code:
/*ID: 15114582PROG: cowxorLANG: C++*/#include<cstdio>#include<cstring>#include<iostream>using namespace std;int bit[111111];bool s[1<<22];int i,j,k,tmp,n,ans,p;void update(int x){ for(int i=1,j=1<<21;i<22;++i,x>>=1,j>>=1)s[x|j]=1;}int main(){ freopen("cowxor.in","r",stdin); freopen("cowxor.out","w",stdout); while(~scanf("%d",&n)) { for(bit[0]=0,i=1;i<=n;++i) scanf("%d",&bit[i]),bit[i]^=bit[i-1]; memset(s,0,sizeof(s)); update(0); ans=bit[p=1]; for(i=1;i<=n;++i) { for(tmp=0,j=1;j<22;++j) { k=(bit[i]>>(21-j))&1; tmp<<=1; if(s[tmp|!k|1<<j])tmp|=!k; else tmp|=k; } tmp^=bit[i]; if(tmp>ans)ans=tmp,p=i; update(bit[i]); } for(i=p;i>0;--i) if((bit[i-1]^bit[p])==ans)break; printf("%d %d %d\n",ans,i,p); } return 0;}