usaco training 6.1.3 Cow XOR 题解

来源:互联网 发布:淘宝限制购买怎么解决 编辑:程序博客网 时间:2024/05/18 03:37

转载请注明出处:http://blog.csdn.net/jiangshibiao/article/details/21441431

【原题】

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 sec

PROGRAM NAME: cowxor

INPUT 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) 


【译题】

描述

农民约翰在喂奶牛的时候被另一个问题卡住了。他的所有N(1 <= N <= 100,000)个奶牛在他面前排成一行(按序号1..N的顺序),按照它们的社会等级排序。奶牛#1有最高的社会等级,奶牛#N最低。每个奶牛同时被指定了一个不唯一的附加值,这个数在0..2^21 - 1的范围内。

帮助农民约翰找出应该从哪一头奶牛开始喂,使得从这头奶牛开始的一个连续的子序列上,奶牛的附加值的异或最大。

如果有多个这样的子序列,选择结尾的奶牛社会等级最高的。如果还不唯一,选择最短的。

[编辑]格式

PROGRAM NAME: cowxor

INPUT FORMAT:

(file cowxor.in)

INPUT FORMAT

第1行:一个单独的整数N。

第2到N + 1行:N个0..2^21 - 1之间的整数,代表每头奶牛的被赋予的数。第j行描述了社会等级j - 1的奶牛。

OUTPUT FORMAT:

(file cowxor.out)

第 1 行: 3个空格隔开的整数,分别为:最大的异或值,序列的起始位置、终止位置。 时限0.5秒

[编辑]SAMPLE INPUT

510542

[编辑]SAMPLE OUTPUT

6 4 5

[编辑]样例输出说明

最大异或值为6,从第4个开始喂,到第5个结束。

4 异或 2 = 6

(100) 异或 (010) = (110)


【分析】这是字母树的经典应用。首先因为是求xor的最大值,可以用前缀和计算xor值,然后n^2枚举即可。

for (i=1;i<=n;i++)  for (j=1;j<i;j++)    if ((sum[i]^sum[j-1])>ans) 就记录;

然而由于范围的关系,我们只能优化这个算法。事实证明,每次枚举到一个i时,我们不必用O(N)的效率去寻找。我们可以构建一个字母树(准确的说是01树),它的分叉只是0或1(也像一棵二叉树)。每次算好一个前缀和时,我就把它拆成二进制并加入字母树。在查找的时候,我只需沿着字母数向下走:设这一位的数字是T,如果当前的K点有一个与T相反的孩子(0--1,1--0),我们就贪心地往那儿走,否则往另一个分叉走。这样得到的异或肯定是最大的。

【代码】

/*PROG:cowxorID:juan1973LANG:C++*/#include<cstdio>using namespace std;const int maxn=750000;             //树的大小。在不超16M的情况下,越大越好!const int maxm=22;int tree[maxn][2],a[maxm],num[maxn],sum[maxn],ans_start,ans_end,ans,i,n,t,cnt,f,x,now,j;
//num[i]表示字母树中以i节点结尾的前缀和的编号。void insert(int k,int step){  if (step==maxm) {num[k]=i;return;}           //走到底层了,就直接返回。  int t=a[step];  if (tree[k][t]>0) insert(tree[k][t],step+1);  else {tree[k][t]=++cnt;insert(cnt,step+1);}}int find(int k,int step){  int t=a[step];  if (tree[k][t^1]>0) return find(tree[k][t^1],step+1);  //贪心地走异或大的  if (tree[k][t]>0) return find(tree[k][t],step+1);      //真的不行走小的  return num[k];                                         //已经到底了,那么当前的一定最优(返回的是编号)。}int main(){  freopen("cowxor.in","r",stdin);  freopen("cowxor.out","w",stdout);  scanf("%d",&n);cnt=0;sum[0]=0;  for (i=1;i<=n;i++)  {    scanf("%d",&x);now=0;sum[i]=sum[i-1]^x;x=sum[i];    while (now<maxm-1){a[++now]=x%2;x/=2;}    for (j=1;j<=now/2;j++) {t=a[j];a[j]=a[now-j+1];a[now-j+1]=t;}     //转二进制并取反    f=find(0,1);                                                      //查找以i为结尾的异或最大值    if ((sum[i]^sum[f])>ans) {ans_start=f+1;ans_end=i;ans=sum[i]^sum[f];}  //记录    insert(0,1);                                                     //插入字母树  }  if (ans==0) printf("0 1 1\n");                                     //特判  else printf("%d %d %d\n",ans,ans_start,ans_end);  scanf("%d",&n);  return 0;}

5 1
原创粉丝点击