zoukankan      html  css  js  c++  java
  • USACO 6.1 Cow XOR

    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)

    5
    1
    0
    5
    4
    2
    

    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) 

    ————————————————————————题解

    首先是死活都没有想出来标解

    然后是造福大众的Analysis

    Cow XOR

    Suppose that we know the k first bits of binary representation of the greatest possible xor. If there exists a sequence such that its xor (which we shall call q) on the first k bits agrees with those -- and the next bit is '1' -- then we know, that k+1'th bit of the greatest xor will be '1', because if not, then q would be greater.

    如果我们知道了最棒的异或二进制前k位,存在一个序列使它的k位是这样的,然后下一位是1(如果我们可以办到),因为如果不是答案就不会最大。

    The solution first computes values xr[], where xr[q] equals xor of all the cows' values from 1 to q - then, xor of the sequence a..b quals xr[b] xor xr[a-1].

    It then runs a loop over all possible exponents e of 2 (starting from 21 downwards), updating arrays pop[] and best[]. It is assumed that after finishing the loop with exponent e (and before entering the loop, with e assumed to be 22), that for every 0 ≤ q ≤ N:

    这个解答先定义变量xr[],这是一个异或前缀和【公子是一个力求简洁的翻译】。然后循环二进制拆位从22开始倒序枚举,然后对于每个0 ≤ q ≤ N

    • xr[pop[q][0]]'s and xr[q]'s binary representations are the same on positions e, e+1, etc., and pop[q][0] is biggest possible with 0 ≤ pop[q][0] < q. If there's no such pop[q][0] possible, then pop[q][0] = -1.
    • xr[pop[q][1]]'s and xr[q]'s binary representations are the same on positions e+1, e+2, etc., different on position e, and pop[q][1] is biggest possible with 0 ≤ pop[q][1] < q. If there's no such pop[q][1] possible, then pop[q][1] = -1.
    • if X would be equal biggest possible xor, then xr[best[q]] xor xr[q]'s in equal X's binary representation on positions e, e+1, etc., and best[q] is biggest possible with 0 ≤ best[q] < q. If there's no such best[q] possible, then best[q] = -1.

    After performing the loop with e = 0, for each 0 ≤ q ≤ N, best[q] = -1 or xr[best[q]] xor xr[q] is equal X. There exists at least one q with best[q] non-negative, because there exist such a and b, a ≤ b, that xr[a-1] xor xr[b] = X.

    The last step will be finding smallest possible q with non-negative best[q], which satisfies, that if there're more possible sequences, we should choose one with last cow having highest rank. The third condition, stating, that if there's still a tie we should choose shortest sequence, is satisfied thanks to the fact, that best[q] is always biggest possible.

    这段话,这么个意思【逐句翻译太累,把重点挑出来】

    【我们现在枚举到的指数是e】

    我们维护个pop[n][2]:

    xr[pop[q][0]]和xr[q]二进制[e,22]位都相等,且pop[q][0]是[0,q)中最大的,若找不到赋值-1

    xr[pop[q][1]]和xr[q]二进制[e+1,22] 位都相等,且pop[q][0]是[0,q)中最大的,若找不到赋值-1

    维护一个best[q]使得在目前情况中[best[q],q]异或的值满足可以达到的[e,22]最大赋值,且best[q]是[0,q)中最大的,若没有best[q]=-1

    最后对所有q扫一遍best[q],得出答案

    然后这三者怎么求……

    我们对于一个best[q]与q,当我们枚举的这一指数e它们异或之后这一位不为1而是0,那么我们就需要考虑更新best[q],因为best[q]存的是xr[q]^xr[best[q]]的[e+1,22]最好结果【事实上也是答案的最好结果】那么我们要改变best[q],设best[q]的新值是y,这个新的xr[y]一定前[e+1,22]位与xr[best[q]]是相同的,这就是pop[q][1]的作用了

    当然,如果e这一位无论如何也无法变成1,我们就跳过这一位

    pop[q][0]辅助推出pop[q][1]

    【别忘了看完这个做法之后下面还有一个很亲民的做法……】

    【恭喜您已获得成就,翻译完USACO6.1的全部Analysis】qwq要是会的话还翻译什么Analysis辣

    代码来一发【我的天吓死我了感谢博客园的恢复功能……】

      1 /*
      2 ID: ivorysi
      3 LANG: C++
      4 PROG: cowxor
      5 */
      6 #include <iostream>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <queue>
     10 #include <set>
     11 #include <vector>
     12 #include <algorithm>
     13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
     14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
     15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
     16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
     17 #define inf 0x5f5f5f5f
     18 #define ivorysi
     19 #define mo 97797977
     20 #define hash 974711
     21 #define base 47
     22 #define fi first
     23 #define se second
     24 #define pii pair<int,int>
     25 #define esp 1e-8
     26 typedef long long ll;
     27 using namespace std;
     28 int pop[100005][2],best[100005],a[100005],xr[100005],n,e,t;
     29 int tmp[2];
     30 void solve() {
     31     scanf("%d",&n);
     32     siji(i,1,n) {
     33         scanf("%d",&a[i]);
     34         xr[i]=xr[i-1]^a[i];
     35         best[i]=i-1;
     36         pop[i][0]=i-1;
     37         pop[i][1]=-1;
     38     }
     39     pop[0][0]=-1;pop[0][1]=-1;best[0]=-1;
     40     e=22;
     41     bool one;
     42     while(e--) {
     43         siji(i,1,n) {
     44             if(pop[i][0]==-1) {
     45                 pop[i][1]=-1;
     46             }
     47             else {
     48                 if(xr[pop[i][0]] >> e != xr[i] >> e) {
     49                     tmp[0]=pop[pop[i][0]][1];
     50                     /*pop[pop[i][0]]已经计算好了,
     51                     xr[pop[pop[i][0]][1]]的[e+1,22]与xr[pop[i][0]]相同
     52                     第e位不同,然而现在这种情况正好需要第e位与xr[pop[i][0]]不同
     53                     */    
     54                     tmp[1]=pop[i][0];
     55                     /*
     56                     正好满足前[e+1,22]位相同,第e位不同 
     57                     */
     58                 }
     59                 else {
     60                     tmp[0]=pop[i][0];
     61                     /*
     62                     正好满足前[e,22]位相同
     63                     */
     64                     tmp[1]=pop[pop[i][0]][1];
     65                     /*pop[pop[i][0]]已经计算好了,
     66                     xr[pop[pop[i][0]][1]]的[e+1,22]与xr[pop[i][0]]相同
     67                     第e位不同,然而现在这种情况正好需要第e位与xr[pop[i][0]]不同
     68                     */    
     69                 }
     70                 pop[i][0]=tmp[0];
     71                 pop[i][1]=tmp[1];
     72             }
     73         }
     74         one=false;
     75         siji(i,1,n) {
     76             if(best[i]>=0) {
     77                 if((xr[best[i]]>>e)%2 != (xr[i]>>e)%2 || pop[best[i]][1]>=0) {
     78                     /*
     79                     如果e位不相同或者还可以找到更新的说明e位可以为1
     80                     */
     81                     one=true;break;
     82                 }
     83             }
     84         }
     85         if(one) {
     86             siji(i,1,n) {
     87                 if(best[i]>=0) {
     88                     if((xr[best[i]]>>e)%2 == (xr[i]>>e)%2) {
     89                         best[i]=pop[best[i]][1];
     90                     }
     91                 }
     92             }
     93 
     94         } 
     95     }
     96     for(t=1;best[t]<0;++t);
     97     printf("%d %d %d
    ",xr[t]^xr[best[t]],best[t]+1,t);
     98 }
     99 int main(int argc, char const *argv[])
    100 {
    101 #ifdef ivorysi
    102     freopen("cowxor.in","r",stdin);
    103     freopen("cowxor.out","w",stdout);
    104 #else
    105     freopen("f1.in","r",stdin);
    106 #endif
    107     solve();
    108     return 0;
    109 }

    第二个做法是我们建一棵01的trie树,插入每一个前缀和,从第21位到第1位

    那么我们就可以每次贪心地去查前缀,如果这一位是0我们就尝试往1查,如果这一位是1我们就尝试往0查

    由于USACO的空间很小,所以动态建一棵树……

     1 /*
     2 ID: ivorysi
     3 LANG: C++
     4 PROG: cowxor
     5 */
     6 #include <iostream>
     7 #include <cstdio>
     8 #include <cstring>
     9 #include <queue>
    10 #include <set>
    11 #include <vector>
    12 #include <algorithm>
    13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
    14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
    15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
    16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
    17 #define inf 0x5f5f5f5f
    18 #define ivorysi
    19 #define mo 97797977
    20 #define hash 974711
    21 #define base 47
    22 #define fi first
    23 #define se second
    24 #define pii pair<int,int>
    25 #define esp 1e-8
    26 typedef long long ll;
    27 using namespace std;
    28 struct node {
    29     node *son[2];
    30     int end;
    31     node() {
    32         memset(son,0,sizeof(son));
    33         end=0;
    34     }
    35 }*root;
    36 int n,a[100005];
    37 int sum[100005];
    38 int s,t,ans,x;
    39 void ins(int val,int id) {
    40     node *p=root;
    41     gongzi(i,20,0) {
    42         x=(val>>i)&1;
    43         if(p->son[x]==0)
    44             p->son[x]=new node;
    45         p=p->son[x];
    46     }
    47     p->end=max(p->end,id);
    48 }
    49 int calc(int val) {
    50     node *p=root;
    51     gongzi(i,20,0) {
    52         x=((val>>i)&1)^1;
    53         if(p->son[x]!=0) {
    54             p=p->son[x];
    55         }
    56         else {
    57             p=p->son[x^1];
    58         } 
    59     }
    60     return p->end;
    61 }
    62 void solve() {
    63     scanf("%d",&n);
    64     siji(i,1,n) scanf("%d",&a[i]);
    65     sum[0]=0;
    66     root=new node;
    67     ins(sum[0],0);
    68     ans=-1;
    69     int pos;
    70     siji(i,1,n) {
    71         sum[i]=sum[i-1]^a[i];
    72         pos=calc(sum[i]);
    73         
    74         if((sum[i]^sum[pos])> ans) {
    75             ans=sum[i]^sum[pos];
    76             s=pos+1;t=i;
    77         }
    78         ins(sum[i],i);
    79     }
    80     printf("%d %d %d
    ",ans,s,t);
    81 }
    82 int main(int argc, char const *argv[])
    83 {
    84 #ifdef ivorysi
    85     freopen("cowxor.in","r",stdin);
    86     freopen("cowxor.out","w",stdout);
    87 #else
    88     freopen("f1.in","r",stdin);
    89 #endif
    90     solve();
    91     return 0;
    92 }
  • 相关阅读:
    SSM实现mysql数据库账号密码加密连接
    获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
    JavaWeb(一) / /* /**的区别
    IDEA(一) 使用IDEA搭建SSM框架项目
    Mysql连接数据库异常汇总【必收藏】
    Java代理模式及动态代理详解
    SpringBoot集成Thymeleaf
    设计师,程序员,当心字体侵权
    Java开发神器Lombok使用详解
    日期格式化跨年bug,是否与你不期而遇?
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6474302.html
Copyright © 2011-2022 走看看